Skip to content

Instantly share code, notes, and snippets.

@seyDoggy
Last active December 31, 2015 23:59
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 seyDoggy/8063508 to your computer and use it in GitHub Desktop.
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.
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