leastOccurrences method
Returns an Occurrence of the least common value and its frequency,
or null if the iterable is empty.
All double.nan values count as one value; ties go to the value seen
first.
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
Occurrence<T>? leastOccurrences() {
// The shared counter folds every NaN into one key (a default map throws
// on NaN) and reads this iterable once, so lazy sources run only once.
final MapEntry<T, int>? leastCommonEntry = pickFrequencyEntry(
frequencyMapOf<T>(this),
// Strict comparison: on a tie the first-seen value wins.
(int candidate, int best) => candidate < best,
);
// No entry means the iterable was empty.
if (leastCommonEntry == null) {
return null;
}
return Occurrence<T>(leastCommonEntry.key, leastCommonEntry.value);
}