Created
July 26, 2017 15:44
-
-
Save aspencer8111/40a6f6c52683f8a16596ed8c4fce7ae5 to your computer and use it in GitHub Desktop.
Using a counter Obj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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