multimode function
Return all the most frequent values of the array, in the order they first
appear. When every value has the same frequency, all of them are returned
(same behavior as Python's statistics.multimode).
Examples
var n = Array([5, 0, 6, 8, 10, 8, 10]);
print(multimode(n));
/* output:
Array([8.0, 10.0])
*/
Implementation
Array multimode(Array a) {
var counts = <double, int>{};
var maxCount = 0;
for (var value in a) {
var count = (counts[value] ?? 0) + 1;
counts[value] = count;
if (count > maxCount) maxCount = count;
}
var modes = Array.empty();
counts.forEach((value, count) {
if (count == maxCount) modes.add(value);
});
return modes;
}