Created
June 17, 2018 20:52
-
-
Save erick2014/fba46bbd9b4f1d4998ff4071d1ad551d to your computer and use it in GitHub Desktop.
Count duplicated elements using Array.prototype.reduce
This file contains 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 = ['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