Skip to content

Instantly share code, notes, and snippets.

@zinthose
Forked from tejashah88/es6-mode.js
Last active April 15, 2021 02:03
Show Gist options
  • Save zinthose/d3f87fdd8807bf6f650731f75916c317 to your computer and use it in GitHub Desktop.
Save zinthose/d3f87fdd8807bf6f650731f75916c317 to your computer and use it in GitHub Desktop.
An ES6 function to compute the mode(s) of an array. All modes found will return sorted in ascending order.
// Credits go to @Anjuna5 for original code: https://stackoverflow.com/a/39838257/9779148
const mode = arr => [...new Set(arr)]
.map(value => [value, arr.filter((v) => v === value).length])
.sort((a,b) => b[1] - a[1])
.filter((v, i, a) => v[1] === a[0][1])
.map(v => v[0])
.sort((a,b) => a - b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment