maymunskoa

Word Occurence (Assoc Arrays)

Mar 5th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     let collection = input.slice(0);
  3.     let words = new Map();
  4.     let uniqWords = Array.from(new Set(input));
  5.  
  6.  
  7.     for (const word of uniqWords) {
  8.             let currentSet = collection.filter(item=> item === word );
  9.             words.set(word,currentSet.length);          
  10.     }
  11.  
  12.                 Array.from(words)
  13.                     .sort((a,b)=>{
  14.                         return b[1] - a[1];
  15.                     }).forEach(pair=>{
  16.                         console.log(`${pair[0]} -> ${pair[1]} times`);        
  17.                     });
  18.  
  19.  
  20. }
Advertisement
Add Comment
Please, Sign In to add comment