countBy<T, K> static method
Counts occurrences in list, optionally grouping by the value of by.
Arr.countBy(['a', 'b', 'a', 'c']); // {'a': 2, 'b': 1, 'c': 1}
Arr.countBy(orders, by: (o) => o.status); // {paid: 12, pending: 3}
Implementation
static Map<K, int> countBy<T, K>(Iterable<T> list, {K Function(T)? by}) {
final result = <K, int>{};
for (final v in list) {
final key = (by != null ? by(v) : v) as K;
result[key] = (result[key] ?? 0) + 1;
}
return result;
}