deno.land / x / simplestatistics@v7.8.3 / src / mode_fast.js
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758/* globals Map: false */
/** * The [mode](https://en.wikipedia.org/wiki/Mode_%28statistics%29) is the number * that appears in a list the highest number of times. * There can be multiple modes in a list: in the event of a tie, this * algorithm will return the most recently seen mode. * * modeFast uses a Map object to keep track of the mode, instead of the approach * used with `mode`, a sorted array. As a result, it is faster * than `mode` and supports any data type that can be compared with `==`. * It also requires a * [JavaScript environment with support for Map](https://kangax.github.io/compat-table/es6/#test-Map), * and will throw an error if Map is not available. * * This is a [measure of central tendency](https://en.wikipedia.org/wiki/Central_tendency): * a method of finding a typical or central value of a set of numbers. * * @param {Array<*>} x a sample of one or more data points * @returns {?*} mode * @throws {ReferenceError} if the JavaScript environment doesn't support Map * @throws {Error} if x is empty * @example * modeFast(['rabbits', 'rabbits', 'squirrels']); // => 'rabbits' */function modeFast(x) { // This index will reflect the incidence of different values, indexing // them like // { value: count } const index = new Map();
// A running `mode` and the number of times it has been encountered. let mode; let modeCount = 0;
for (let i = 0; i < x.length; i++) { let newCount = index.get(x[i]); if (newCount === undefined) { newCount = 1; } else { newCount++; } if (newCount > modeCount) { mode = x[i]; modeCount = newCount; } index.set(x[i], newCount); }
if (modeCount === 0) { throw new Error("mode requires at last one data point"); }
return mode;}
export default modeFast;
Version Info