Skip to content

Instantly share code, notes, and snippets.

@aspencer8111
Created July 26, 2017 15:44
Show Gist options
  • Save aspencer8111/40a6f6c52683f8a16596ed8c4fce7ae5 to your computer and use it in GitHub Desktop.
Save aspencer8111/40a6f6c52683f8a16596ed8c4fce7ae5 to your computer and use it in GitHub Desktop.
Using a counter Obj
var arr = [1, 1, 3, 4, 6, 4, 5, 7, 89, 90, 0, 5, 32, 1, 1, 1]
var mostOccurances = function(arr){
var counterObj = {}
for(var i=0; i<arr.length; i++){
var num = arr[i]
if(counterObj[num] == undefined){
counterObj[num] = 1
} else {
counterObj[num] = counterObj[num] + 1
}
}
return counterObj
}
console.log(mostOccurances(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment