ALSAVOV

Untitled

Feb 9th, 2024
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     const wordsOccurence = {};
  3.     const wordsToCheck = input.shift().split(" "); // ['this', 'sentence']
  4.     let counter = 0;
  5.  
  6.     for (const word of wordsToCheck) {
  7.         for (const str of input) {
  8.             if (word == str) {
  9.                 if (!wordsOccurence.hasOwnProperty(word)) {
  10.                     counter++;
  11.                 } else {
  12.                     counter = wordsOccurence[word];
  13.                     counter++;
  14.                 }
  15.  
  16.                 wordsOccurence[word] = counter;
  17.                 counter = 0;
  18.             }
  19.         }
  20.     }
  21.  
  22.     let sorted = Object.entries(wordsOccurence).sort((a, b) => b[1] - a[1]);
  23.     for (const [word, count] of sorted) {
  24.         console.log(`${word} - ${count}`);
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment