Skip to content

Instantly share code, notes, and snippets.

@tejashah88
Created June 6, 2018 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tejashah88/bdd833a8d686203094ccd4245f6f6255 to your computer and use it in GitHub Desktop.
Save tejashah88/bdd833a8d686203094ccd4245f6f6255 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
// 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]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment