mode function

num mode(
  1. List array
)

Computes the value that appears most frequently in array.

Implementation

num mode(List<dynamic> array) {
  var counts = <int, int>{};
  for (var n in array) {
    counts[n] = (counts[n] ?? 0) + 1;
  }
  int? maxCount = 0;
  num? maxValue;
  for (var n in counts.keys) {
    if (counts[n]! > maxCount!) {
      maxCount = counts[n];
      maxValue = n;
    }
  }
  return maxValue ?? 0;
}