Skip to content

Instantly share code, notes, and snippets.

@jaabiri
Forked from elijahmanor/findTop10.js
Created September 1, 2018 00:54
Show Gist options
  • Save jaabiri/a77aadb357e3e0618ca33bc33369481b to your computer and use it in GitHub Desktop.
Save jaabiri/a77aadb357e3e0618ca33bc33369481b to your computer and use it in GitHub Desktop.
findTop10.js
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