Advertisement
dilyana2001

Untitled

Jul 18th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wordOccurrences(strArr) {
  2.     let words = new Map()
  3.     for (let word of strArr) {
  4.         let times = 1
  5.         if (words.has(word)) {
  6.             let currentTimes = words.get(word)
  7.             let newTimes = currentTimes + times
  8.             words.set(word, newTimes)
  9.         } else {
  10.             words.set(word, times)
  11.         }
  12.     }
  13.     let sorted = Array.from(words).sort((a, b) => b[1] - a[1])
  14.     for (let el of sorted) {
  15.         console.log(`${el[0]} -> ${el[1]} times`)
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement