Skip to content

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.

any[]

a sample of one or more data points

any

mode

if the JavaScript environment doesn’t support Map

if x is empty

modeFast(['rabbits', 'rabbits', 'squirrels']); // => 'rabbits'