Skip to content

Instantly share code, notes, and snippets.

@aclave1
Created September 1, 2015 14:58
Show Gist options
  • Save aclave1/a68fdf84e27c031231f2 to your computer and use it in GitHub Desktop.
Save aclave1/a68fdf84e27c031231f2 to your computer and use it in GitHub Desktop.
count the number of times an element occurs in an array
[1,2,3,1,1,2,3,4].reduce(function(map,val){
map[val] = typeof map[val] !== 'undefined' ? map[val]+1 : 1;
return map;
},{});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment