-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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