countBy<TKey> method

Iterable<MapEntry<TKey, int>> countBy<TKey>(
  1. TKey keySelector(
    1. T element
    )
)

Applies keySelector to every element in this iterable and returns an iterable containing each resulting key and the number of times that key appears in this iterable.

The order of the resulting iterable is not guaranteed to be any particular order.

Implementation

Iterable<MapEntry<TKey, int>> countBy<TKey>(
  TKey Function(T element) keySelector,
) {
  final countMap = HashMap<TKey, int>();
  for (var o in this) {
    final key = keySelector(o);
    countMap[key] = (countMap[key] ?? 0) + 1;
  }
  return countMap.entries;
}