Advertisement
Lulunga

02. Odd Occurrences Associative Arrays

Jul 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function oddOccurences(input) {
  2.     let words = input.split(' ').map(a => a.toLowerCase());
  3.     let extracted = [];
  4.  
  5.     for (let word of words) {
  6.         if (countOdd(words, word)) {
  7.             extracted.push(word);
  8.         }
  9.     }
  10.     extracted = [...new Set(extracted)];
  11.     console.log(extracted.join(' '));
  12.  
  13.     function countOdd(array, value) {
  14.         var n = 0;
  15.         for (i = 0; i < array.length; i++) {
  16.             if (array[i] == value) { n++ }
  17.         }
  18.         return n % 2 === 1;
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement