distinctBy<K> method

List<T> distinctBy<K>(
  1. K key(
    1. T
    )
)

Removes duplicates by key and returns a new list.

Implementation

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