-
-
Save jaabiri/a77aadb357e3e0618ca33bc33369481b to your computer and use it in GitHub Desktop.
findTop10.js
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
const list = [ | |
"this", | |
"is", | |
"a", | |
"test", | |
"which", | |
"word", | |
"wins", | |
"top", | |
"i", | |
"don't", | |
"know", | |
"off", | |
"hand", | |
"do", | |
"you", | |
"this", | |
"a", | |
"a", | |
"this", | |
"test", | |
"a", | |
"a", | |
"do", | |
"hand", | |
"hand", | |
"a", | |
"whatever", | |
"what", | |
"do", | |
"do" | |
]; | |
const findTop10 = list => { | |
const frequency = list.reduce((memo, item) => { | |
if (memo[item]) { | |
memo[item] += 1; | |
} else { | |
memo[item] = 1; | |
} | |
return memo; | |
}, {}); | |
return Object.keys(frequency) | |
.map(key => ({ key, value: frequency[key] })) | |
.sort((a, b) => b.value - a.value) | |
.splice(0, 10); | |
}; | |
console.clear(); | |
console.log(findTop10(list)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment