Last active
March 17, 2018 17:39
-
-
Save vadimkorr/47ed5a7a8f4d13ef4852980e7c216220 to your computer and use it in GitHub Desktop.
Find first non repeating char in a string
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
| // 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