Todorov_Stanimir

02. Odd Occurrences with Map

Jul 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function oddOccurrences(input) {
  2.     let output = new Map();
  3.     output.set('oddTimes', []);
  4.     input = input.split(' ').map(el => el = el.toLowerCase());
  5.  
  6.     input.forEach(word => {
  7.         let counter = input.slice(0).filter(element => element === word).length
  8.         if (counter % 2 !== 0 && !(output.get('oddTimes').includes(word))) {
  9.             output.get('oddTimes').push(word);
  10.         }
  11.     });
  12.  
  13.     console.log(output.get('oddTimes').join(' '));
  14. }
  15. oddOccurrences('Java C# Php PHP Java PhP 3 C# 3 1 5 C#')
Advertisement
Add Comment
Please, Sign In to add comment