Count unique elements in list [closed] - list

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
For example the result for
mylist = [1, 1, 4, 0, 4, 4]
I want to
a = [1,4,0]
b = [2,3,1]

final mylist = [1, 1, 4, 0, 4, 4];
final result = mylist
.fold(<int, int>{}, (Map<int, int> map, item) => map..update(item, (count) => count + 1, ifAbsent: () => 1));
print(result.keys.toList());
print(result.values.toList());

Another way:
void main(List<String> args) {
final mylist = [1, 1, 4, 0, 4, 4];
var counter = Map<int, int>.fromEntries(mylist.toSet().map<MapEntry<int, int>>((k) => MapEntry(k, mylist.where((e) => e == k).length)));
print(counter.keys);
print(counter.values);
}

Related

How to get unique numbers from two lists in dart?

void main() {
List<int> x = [1,4,3,6];
List<int> y = [3,6,9,10,2];
var List<int> output = [];
print(output);
}
I don't know what exactly "unique numbers" mean in your context.
I guest "unique numbers" from list x an y mean the numbers which are in x or in y.
Example: x = [1, 2, 3], y = [2, 3, 4], the output is [1, 2, 3, 4]
or x = [3, 4, 5] and y = [5, 6, 7] => ouput = [3, 4, 5, 6, 7]
If this is the result you want to archive, you can consider following code:
void main() {
List<int> x = [1,4,3,6];
List<int> y = [3,6,9,10,2];
List<int> output = x.toSet().union(y.toSet()).toList();
print(output);
}
output:
[1, 4, 3, 6, 9, 10, 2]
I am javascript developer but that would help you
Var uniqueArray =list1.filter(function(obj){return list2.indexOf(obj)== -1;})

How to get the index of a sorted list which has reoccurring numbers

I have a list, say [2,3,3,4], I want to return the index of the list as [0, 1, 1, 3]. Any idea on how to do this?
Thanks
You can achieve this with np.unique as follows:
import numpy as np
a = [2, 3, 3, 4]
b = [0, 1, 1, 3]
us, inds, inv = np.unique(a, return_index=True, return_inverse=True)
res = inds[inv]
assert (b == res).all()

Expand the size of a List by N

I am having difficulty in phrasing this question so I apologies for the vague sounding title.
I started learning Scala today. I would like to generate a list of elements, and then multiply that list by a factor N, with the result as follows:
List(1, 2, 3, 4) * N -> List(1, 2, 3, 4, 1, 2, 3, 4) (where N = 2)
In Python, I would just do the following which would yield what I am looking for:
my_list = [1, 2, 3, 4] * 2
However, this does not work in Scala.
Try List.fill
List.fill(2)(List(1, 2, 3, 4)).flatten
// : List[Int] = List(1, 2, 3, 4, 1, 2, 3, 4)
or provide extension method to mimic Python, something like
extension (l: List[Int])
def *(n: Int): List[Int] = List.fill(n)(l).flatten
List(1, 2, 3, 4) * 2
// : List[Int] = List(1, 2, 3, 4, 1, 2, 3, 4)
You can do something like that:
val l = List(1, 2, 3, 4)
val n = 2
val result = (1 to n)
.flatMap(_ => l)
.toList
Another option you have is:
val l = List(1, 2, 3, 4)
val length = l.length
List.tabulate(length * n)(i => l(i % length))

Dart: split a list to two sublists based on condition

Have there any way to split a list in dart based on a condition like following:
[1, 2, 3, 4, 5, 6, 7, 8] (A sample list)
After splitting it based on i % 2 == 0 condition,
it would generate the following two lists:
1) [1, 3, 5, 7]
2) [2, 4, 6, 8]
I know I can simply write a loop to go through all the elements and check the condition to create the two sublists. But have there any shorter functional way in dart?
Thanks in advance!
If you want to do this a lot it could be a good idea to create an extension method in your project which does what you want. I have come up with the following design which should work in a generic and efficient way:
void main() {
final s_list = [1, 2, 3, 4, 5, 6, 7, 8];
final match = s_list.splitMatch((element) => element % 2 == 0);
print(match.matched); // [2, 4, 6, 8]
print(match.unmatched); // [1, 3, 5, 7]
}
extension SplitMatch<T> on List<T> {
ListMatch<T> splitMatch(bool Function(T element) matchFunction) {
final listMatch = ListMatch<T>();
for (final element in this) {
if (matchFunction(element)) {
listMatch.matched.add(element);
} else {
listMatch.unmatched.add(element);
}
}
return listMatch;
}
}
class ListMatch<T> {
List<T> matched = <T>[];
List<T> unmatched = <T>[];
}
A quick solution:
var s_list = [1, 2, 3, 4, 5, 6, 7, 8];
s_list.where( (el) => el % 2 == 0 ).toList();
s_list.where( (el) => el % 2 != 0 ).toList();

Is there a Groovy equivalent to Ruby's #map?

I realize there is support for #each
Book.findAll().each(){ book->
println ">>> ${book}"
}
and there's even support for #inject
def sentence = m.inject('Message: ') { s, k, v ->
s += "${k == 'likes' ? 'loves' : k} $v "
}
Is there support for #map for Groovy out of the box (without any special libraries like Functional Java)?
def list = [1,2,3,4].map{ num->
num + 1
}
assert list == [2,3,4,5]
You want collect.
groovy:000> [1,2,3,4].collect { num -> num + 1 }
===> [2, 3, 4, 5]
I hope that helps.
You can use collect, as in
[1, 2, 3, 4].collect { it + 1 }
For the case where you're calling a method directly on each object in the collection there's a shorter syntax using the spread-dot operator:
[1, 2, 3, 4]*.plus 1
(using a method Groovy adds to java.lang.Integer to implement the + operator)
This operator works even if the list contains nulls:
groovy:000> [1, 2, 3, null, 4]*.plus 1
===> [2, 3, 4, null, 5]
where with collect you'd have to check:
[1, 2, 3, null, 4].collect { it?.plus 1 }