Created
October 24, 2014 22:58
-
-
Save sergeevabc/b4f6c93d9cc1461ce338 to your computer and use it in GitHub Desktop.
Deduplicate chars in string nLpMLCkDfrDdnLpMLCkDfrDd -> nLpMCkDfr
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
// Maxdamntus.forEach | |
function dedupString(string) { | |
var acc = { | |
str: "", | |
map: {} | |
}; | |
Array.prototype.forEach.call(string, function(char) { | |
if (!acc.map[char.toLowerCase()]) acc.str += char; | |
acc.map[char.toLowerCase()] = true; | |
}); | |
return acc.str; | |
} | |
// Maxdamantus.reduce | |
function dedupString(string) { | |
return Array.prototype.reduce.call(string, function(acc, char) { | |
return acc.indexOf(char.toLowerCase()) < 0 ? acc + char.toLowerCase() : acc; | |
}, ""); | |
} | |
// Ljharb.reduce | |
function dedupString(string) { | |
return Array.prototype.reduce.call(string, function(acc, char) { | |
if (!acc.map[char.toLowerCase()]) { | |
acc.str += char; | |
} | |
acc.map[char.toLowerCase()] = true; | |
return acc; | |
}, { | |
str: '', | |
map: {} | |
}).str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment