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.
25 lines (24 loc) • 697 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findMostFrequent = void 0;
/**
* Returns the most frequently occurring element(s) in the array.
* @author @dailker
* @template T
* @param {T[]} array
* @returns {T[]}
*/
function findMostFrequent(array) {
const freq = new Map();
let max = 0;
for (const item of array) {
const count = (freq.get(item) ?? 0) + 1;
freq.set(item, count);
if (count > max)
max = count;
}
return Array.from(freq.entries())
.filter(([_, count]) => count === max)
.map(([item]) => item);
}
exports.findMostFrequent = findMostFrequent;