naeemqaswar
7/18/2019 - 4:12 PM

Max Ocurrence Number

function maxOccurenceNum(a) {

  let max = { count: 0, num: -1 }, tmpCount = 0;

  a.sort().forEach((n, i)=>{
    if(i !== 0 && a[i] === a[i-1]){
      tmpCount ++;
    } else {
      if(tmpCount > max.count){
        max.count = tmpCount;
        max.num = a[i-1];
      }
      tmpCount = 1;
    }
  });

  return max.num;
}