modeFast
modeFast(
x):any
Defined in: src/mode_fast.js:28
The mode is the number
that appears in a list the highest number of times.
There can be multiple modes in a list. This algorithm walks the input in
order, so a tie resolves to whichever tied value reaches the highest count
first. mode and modeSorted break ties differently, returning the
smallest of the tied values.
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,
and will throw an error if Map is not available.
This is a measure of central tendency: a method of finding a typical or central value of a set of numbers.
Parameters
Section titled “Parameters”any[]
a sample of one or more data points
Returns
Section titled “Returns”any
mode
Throws
Section titled “Throws”if the JavaScript environment doesn’t support Map
Throws
Section titled “Throws”if x is empty
Example
Section titled “Example”modeFast(['rabbits', 'rabbits', 'squirrels']); // => 'rabbits'