Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wordOccurrences(strArr) {
- let words = new Map()
- for (let word of strArr) {
- let times = 1
- if (words.has(word)) {
- let currentTimes = words.get(word)
- let newTimes = currentTimes + times
- words.set(word, newTimes)
- } else {
- words.set(word, times)
- }
- }
- let sorted = Array.from(words).sort((a, b) => b[1] - a[1])
- for (let el of sorted) {
- console.log(`${el[0]} -> ${el[1]} times`)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement