mostOccurrences method

MapEntry<Enum, int> mostOccurrences()

Return a MapEntry with the most common value and its frequency.

Dart’s type system doesn’t allow us to return a strongly typed enum from a method that works with the base Enum type. This is because Dart’s generics are invariant, which means you can’t use a subtype (like ZodiacSigns) where a base type (like Enum) is expected.

Implementation

MapEntry<Enum, int> mostOccurrences() {
  // If the list is empty, the method will now throw an exception with
  // a descriptive error message.
  if (isEmpty) {
    throw Exception('Error: The list is empty.');
  }

  // Create a new HashMap to store each integer and its frequency.
  // In Dart, Map is an interface that HashMap implements. Using Map makes
  // code more flexible, as it can work with any class that implements Map.
  final Map<Enum, int> frequencyMap = <Enum, int>{};

  // Iterate over each integer in the list.
  for (final Enum 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: require_future_error_handling
    frequencyMap.update(item, (int value) => value + 1, ifAbsent: () => 1);
  }

  // Find and return the key with the highest value (frequency) in the map.
  // The map is guaranteed non-empty since we checked isEmpty above.
  MapEntry<Enum, int>? mostCommonEntry;
  for (final MapEntry<Enum, int> entry in frequencyMap.entries) {
    if (mostCommonEntry == null || entry.value > mostCommonEntry.value) {
      mostCommonEntry = entry;
    }
  }

  // Return a MapEntry with the most common value and its frequency.
  // mostCommonEntry is guaranteed non-null since the list is non-empty.
  if (mostCommonEntry == null) {
    throw StateError('Unexpected null entry in non-empty frequency map');
  }
  return mostCommonEntry;
}