Vassil_Iliev

most-ocuring

Mar 22nd, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const arrThree = [1, 2, 1, 4, 2, 4, 2, 3, 3, 5, 6, 7, 3, 2];
  2.  
  3. arrThree.sort((a, b) => a - b);
  4. console.log(arrThree);
  5.  
  6. let length = 0;
  7. let biggestLength = 0;
  8. let mostFreq;
  9.  
  10. for (let i = 1; i <= arrThree.length; i++) {
  11.  
  12.     if (arrThree[i - 1] === arrThree[i]) {
  13.         length++
  14.     } else {
  15.  
  16.         if (length > biggestLength) {
  17.             biggestLength = length
  18.             length = 0;
  19.             mostFreq = arrThree[i - 1]
  20.         } else {
  21.             length = 0;
  22.         }
  23.     }
  24. }
  25. console.log(`${mostFreq} (${biggestLength + 1} times)`)
Advertisement
Add Comment
Please, Sign In to add comment