Created
May 25, 2021 23:23
-
-
Save scolton99/c7fde0a9d4acb3eaec6875cb556c2a5b to your computer and use it in GitHub Desktop.
My solution to the word frequency problem in JavaScript.
This file contains hidden or 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 input = ["apple", "apple", "orange"]; | |
| const itemFreq = {}; | |
| for (const s of input) { | |
| if (!(s in itemFreq)) | |
| itemFreq[s] = 0; | |
| itemFreq[s]++; | |
| } | |
| const filtered = input.sort((x, y) => { itemFreq[x] < itemFreq[y] ? 1 : itemFreq[x] > itemFreq[y] ? -1 : 0 }) | |
| .filter((x, i) => input.indexOf(x) === i); | |
| for (const s of filtered) | |
| console.log(`${s}: ${itemFreq[s]}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment