leastOccurrences method
Returns an Occurrence of the least common value and its frequency,
or null if the iterable is empty.
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
Occurrence<int>? leastOccurrences() {
// check if the list is empty before calling reduce
if (isEmpty) {
return null;
}
// Create a new HashMap to store each integer and its frequency.
final HashMap<int, int> frequencyMap = HashMap<int, int>();
// Iterate over each integer in the list.
for (final int item in this) {
// Update the frequency of the current integer in the map, or
// set it to 1 if it's not in the map yet.
// ignore: saropa_lints/require_future_error_handling -- Map.update is synchronous; there is no future to handle
frequencyMap.update(item, (int value) => value + 1, ifAbsent: () => 1);
}
// Find the key-value pair with the lowest value (frequency) in the map.
// The map is guaranteed non-empty since we checked isEmpty above.
MapEntry<int, int>? leastCommonEntry;
for (final MapEntry<int, int> entry in frequencyMap.entries) {
if (leastCommonEntry == null || entry.value < leastCommonEntry.value) {
leastCommonEntry = entry;
}
}
if (leastCommonEntry == null) {
return null;
}
return Occurrence<int>(leastCommonEntry.key, leastCommonEntry.value);
}