Created
April 21, 2017 13:45
-
-
Save thinkphp/1e1fc93d459022d6a87e4ca1f0564d97 to your computer and use it in GitHub Desktop.
Find Duplicates using 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 = [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