mostOccurrences method

  1. @useResult
Occurrence<double>? mostOccurrences()

Finds the most common value in the list.

Returns an Occurrence containing the most common value and its frequency. If the list is empty, returns null.

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<double>? mostOccurrences() {
  // 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<double, int>? mostCommonEntry = pickFrequencyEntry(
    frequencyMapOf<double>(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 (mostCommonEntry == null) {
    return null;
  }

  return Occurrence<double>(mostCommonEntry.key, mostCommonEntry.value);
}