Created
May 7, 2013 10:31
-
-
Save sergi/5531711 to your computer and use it in GitHub Desktop.
Find the first unrepeated char in a string.
http://programmingpraxis.com/2013/04/30/first-unrepeated-character-in-a-string/
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
function firstUnrepeated(str) { | |
var blacklist = []; | |
var once = {}; | |
str.split("").forEach(function(e, pos) { | |
if (blacklist.indexOf(e) === -1) { | |
once[e] = pos; | |
blacklist.push(e); | |
} | |
else { delete once[e]; } | |
}); | |
return Object.keys(o).reduce(function(prev, cur) { | |
if (!o[prev] || o[cur][1] < o[prev][1]) | |
return cur; | |
return prev; | |
}, null) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment