maymunskoa

01. Word Tracker (Assoc Arrays)

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