distinctByComparison method

Set<T> distinctByComparison(
  1. bool comparison(
    1. T,
    2. T
    )
)

Removes duplicate elements from a list based on a provided comparison function.

Implementation

Set<T> distinctByComparison(bool Function(T, T) comparison) {
  Set<T> uniqueElements = {};
  for (T element in this) {
    if (!uniqueElements.any((e) => comparison(e, element))) {
      uniqueElements.add(element);
    }
  }
  return uniqueElements;
}