Advertisement
tetris555

odd-occurrences

Nov 4th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve2(str) {
  2.     str = str.split(' ')
  3.         .map(x => x.toLowerCase());
  4.        
  5.     const wordCounter = {};
  6.  
  7.     // initialize the wordCounter
  8.     for (const el of Array.from(new Set([...str]))) {
  9.         wordCounter[el] = 0;
  10.     }
  11.  
  12.     // populate the wordCounter
  13.     for (const word of str) {
  14.         if (wordCounter.hasOwnProperty(word)) {
  15.             wordCounter[word]++;
  16.         }
  17.     }
  18.  
  19.     // sort and print
  20.     const countOdd = [...Object.entries(wordCounter)].filter(([k, v]) => v % 2 === 1); // [[c#, 3], ...]
  21.     console.log(countOdd.map(e => e[0]).join(' '));
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement