Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 21, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/1e1fc93d459022d6a87e4ca1f0564d97 to your computer and use it in GitHub Desktop.
Save thinkphp/1e1fc93d459022d6a87e4ca1f0564d97 to your computer and use it in GitHub Desktop.
Find Duplicates using reduce
var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
var arr = arr.reduce(function(acc,curr){
acc[curr] ? acc[curr]++ : acc[curr]=1
return acc
},{})
console.log(arr);
var str = "ABRACADABRA",
obj = {}
for(var i = 0; i < str.length; ++i) {
obj[str[i]] ? obj[str[i]]++ : obj[str[i]] = 1
}
console.log(obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment