Last active
December 31, 2015 23:59
-
-
Save seyDoggy/8063508 to your computer and use it in GitHub Desktop.
I've never worked through my own solution to the infamous "First Unique Character" problem.
This file contains hidden or 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
function firstUnique (str) { | |
var hash = {}; | |
for (var i = 0; i < str.length; i++) { | |
hash[str[i]] = hash[str[i]] + 1 || 1; | |
} | |
for (var i = 0; i < str.length; i++) { | |
if (hash[str[i]] === 1) { | |
return str[i]; | |
} | |
} | |
} | |
console.log(firstUnique('abdaccdz') === 'b');// true | |
console.log(firstUnique('abaabcad') === 'c');// true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment