Advertisement
CR7CR7

repeatingNumbers

Sep 10th, 2023 (edited)
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let input = [
  2.     '4',
  3.     '1',
  4.     '3',
  5.     '3',
  6.     '7'
  7.  
  8.  
  9. ]
  10.  
  11. let print = this.print || console.log;
  12. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  13.  
  14. let n = +gets();
  15.  
  16. let newArr = [];
  17. let map = new Map();
  18.  
  19. for (let i = 1; i <= n; i++) {
  20.     let numb = +gets(); // convert string to number
  21.     newArr.push(numb);
  22.  
  23.     if (map.has(numb)) {
  24.       map.set(numb, map.get(numb) + 1);
  25.     } else {
  26.       map.set(numb, 1);
  27.     }
  28. }
  29.  
  30. let maxFreq = 0;
  31. let result = 0;
  32.  
  33. for (let [num, freq] of map.entries()) {
  34.   if (freq > maxFreq || (freq === maxFreq && num < result)) {
  35.    
  36.     maxFreq = freq;
  37.     result = num;
  38.   }
  39. }
  40.  
  41. print(result);
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement