Skip to content

Instantly share code, notes, and snippets.

@vadimkorr
Last active March 17, 2018 17:39
Show Gist options
  • Select an option

  • Save vadimkorr/47ed5a7a8f4d13ef4852980e7c216220 to your computer and use it in GitHub Desktop.

Select an option

Save vadimkorr/47ed5a7a8f4d13ef4852980e7c216220 to your computer and use it in GitHub Desktop.
Find first non repeating char in a string
// Find first non repeating char in a string
function firstUnique(str) {
let dict = {};
for (let i=0; i<str.length; i++) {
if (dict[str[i]]) {
dict[str[i]] += 1;
} else {
dict[str[i]] = 1;
}
}
let keys = Object.keys(dict);
for (let i=0; i<keys.length; i++) {
if (dict[keys[i]] == 1) {
return keys[i];
break;
}
}
}
let str = 'bcabcdf';
console.log('res = ' + firstUnique(str));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment