everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
24 lines (23 loc) • 603 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mode = void 0;
/**
* Finds the mode(s) in a numeric array.
* @author @dailker
* @param {number[]} array
* @returns {number[]}
*/
function mode(array) {
const freq = new Map();
let max = 0;
for (const n of array) {
const count = (freq.get(n) ?? 0) + 1;
freq.set(n, count);
if (count > max)
max = count;
}
return Array.from(freq.entries())
.filter(([_, count]) => count === max)
.map(([n]) => n);
}
exports.mode = mode;