leastOccurrences method

  1. @useResult
Occurrence<bool>? leastOccurrences()

Finds the least common value in the list.

Returns an Occurrence containing the least common value and its frequency. If the list is empty, returns null. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
Occurrence<bool>? leastOccurrences() {
  if (isEmpty) {
    return null;
  }

  final int trueCount = countTrue;
  final int falseCount = length - trueCount;

  // When counts are equal, false is returned (consistent behavior)
  if (falseCount <= trueCount) {
    return Occurrence<bool>(false, falseCount);
  }

  return Occurrence<bool>(true, trueCount);
}