Q:

Write a JavaScript program to find the number which appears most in a given array of integers

0

Write a JavaScript program to find the number which appears most in a given array of integers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function array_element_mode(arr) {
  var ctr = [],
    ans = 0;

  for(var i = 0; i < 10; i++) {
    ctr.push(0);
  }
  for(var i = 0; i < arr.length; i++) {
    ctr[arr[i] - 1]++;
    if(ctr[arr[i] - 1] > ctr[ans]) {
      ans = arr[i] - 1;
    }
  }
  return ans + 1;  
}
console.log(array_element_mode([1, 2, 3, 2, 2, 8, 1, 9]))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now