Created
March 28, 2018 06:38
-
-
Save msaxena25/42d6c47023f28ff01b73ce787da1601a to your computer and use it in GitHub Desktop.
How could you find the first non repeating char 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
//First non repeating char : How could you find the first non repeating char in a string? | |
function firstNonRepeatChar(s) { | |
for (var i = 0; i < s.length; i++) { | |
var letter = s.charAt(i); | |
if (s.substring(s.indexOf(letter) + 1, s.length).indexOf(letter) == -1) { | |
console.log(letter); | |
break; | |
} | |
} | |
} | |
var text = "the quick brown fox jumps then quickly blow air"; | |
firstNonRepeatChar(text); | |
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
//First non repeating char : How could you find the first non repeating char in a string? | |
function firstNonRepeatChar1(s) { | |
for (var i = 0; i < s.length; i++) { | |
var letter = s.charAt(i); | |
if(s.lastIndexOf(letter) == s.indexOf(letter)){ | |
console.log(letter); | |
break; | |
} | |
} | |
} | |
firstNonRepeatChar1('QQQQQ'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment