Skip to content

Instantly share code, notes, and snippets.

@ryankeiper
Last active August 29, 2015 14:21
Show Gist options
  • Save ryankeiper/43e54eab0412fae93d9c to your computer and use it in GitHub Desktop.
Save ryankeiper/43e54eab0412fae93d9c to your computer and use it in GitHub Desktop.

Challenge: Find the number that appears most frequently in the array!

Use Javascript to find which number in the starter code array is the most common.

Good luck!

var numbers = [1,3,73,5,3,7,9,6,4,6,8,0,8,6,5,4,32,4,5,6,8,9,0,9,7,6,4,3,2,4,6,7,7,9,6,5,3,4,6,7,8,0,0,9,7,4,4,3,2,3,4,5,6,7,8,9,0,0,9,8,6,4,1];
var occurences = [];
var incrementor = 0;
var max_val;
var find_most_frequent = function(){
for(var i = 0; i < numbers.length; i++){
for(var oc = 0; oc < numbers.length; oc++){
if(numbers[i] == numbers[oc]){
incrementor ++;
}
}
occurences.push(incrementor);
incrementor = 0;
}
max_val = numbers[occurences.indexOf(Math.max.apply(Math, occurences))];
return max_val;
}
console.log(find_most_frequent());
var numbers = [1,3,73,5,3,7,9,6,4,6,8,0,8,6,5,4,32,4,5,6,8,9,0,9,7,6,4,3,2,4,6,7,7,9,6,5,3,4,6,7,8,0,0,9,7,4,4,3,2,3,4,5,6,7,8,9,0,0,9,8,6,4,1];
// Place solution here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment