Advertisement
Guest User

wordOccurrences2

a guest
Dec 2nd, 2020
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wordOccurrences(arr) {
  2.     let map = new Map();
  3.     for (let word of arr) {
  4.         if (!map.has(word)) {
  5.             map.set(word, 0);
  6.         }
  7.         map.set(word, map.get(word) + 1);
  8.     }
  9.     let sorted = Array.from(map);
  10.     sorted.sort((a, b) => b[1] - a[1]);
  11.     for (let [word, words] of sorted) {
  12.         console.log(`${word} -> ${words} times`);
  13.     }
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement