distinctBy<K> method

  1. @useResult
List<T> distinctBy<K>(
  1. K keyOf(
    1. T
    )
)

Distinct elements by key; keeps first occurrence of each key.

Implementation

@useResult
List<T> distinctBy<K>(K Function(T) keyOf) {
  final Set<K> seen = <K>{};
  final List<T> result = <T>[];
  for (final T element in this) {
    final K key = keyOf(element);
    if (seen.add(key)) result.add(element);
  }
  return result;
}