locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
35 lines (34 loc) • 1.17 kB
JavaScript
export function mode(data) {
// discuss at: https://locutus.io/python/statistics/mode/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Returns the first encountered most-common value.
// example 1: mode([1, 1, 2, 2, 3])
// returns 1: 1
// example 2: mode(['red', 'blue', 'red', 'green'])
// returns 2: 'red'
// example 3: mode([true, false, true])
// returns 3: true
const values = assertStatisticsArray(data, 'mode');
if (values.length === 0) {
throw new Error('no mode for empty data');
}
let bestValue = values[0];
let bestCount = 0;
const counts = new Map();
for (const value of values) {
const count = (counts.get(value) ?? 0) + 1;
counts.set(value, count);
if (count > bestCount) {
bestCount = count;
bestValue = value;
}
}
return bestValue;
}
function assertStatisticsArray(data, functionName) {
if (!Array.isArray(data)) {
throw new TypeError(`${functionName}() data must be an array`);
}
return data;
}