String first singularity checker
Returns the first instance of a character that occurs only once in any given string.
by jmayes
JavaScript
/* It is naughty as it uses associative arrays which can be bad, but I think this is fastest in this special case */
function find(aString) {
var letters = {};
var x = aString.length;
while(x--) {
if(letters[aString.charAt(x)] != null) {
delete letters[aString.charAt(x)];
} else {
letters[aString.charAt(x)] = {'count': 1, 'time': x};
}
}
var keys = Object.keys(letters);
if(letters[keys[0]].time < letters[keys[1]].time) {
return keys[0];
} else {
return keys[1];
}
}
console.log(find('abcdefabcdefijkhijkz'));