Skip to content

Instantly share code, notes, and snippets.

@erick2014
Created June 17, 2018 20:52
Show Gist options
  • Save erick2014/fba46bbd9b4f1d4998ff4071d1ad551d to your computer and use it in GitHub Desktop.
Save erick2014/fba46bbd9b4f1d4998ff4071d1ad551d to your computer and use it in GitHub Desktop.
Count duplicated elements using Array.prototype.reduce
var arr = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
return arr.reduce(function(acc, cur) {
acc[cur] = (acc[cur] || 0) + 1;
return acc;
},{});
// prints { a: 3, b: 2, c: 2, d: 2, e: 2, f: 1, g: 1, h: 3 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment