Skip to content

Instantly share code, notes, and snippets.

@msaxena25
Created March 28, 2018 06:38
Show Gist options
  • Save msaxena25/42d6c47023f28ff01b73ce787da1601a to your computer and use it in GitHub Desktop.
Save msaxena25/42d6c47023f28ff01b73ce787da1601a to your computer and use it in GitHub Desktop.
How could you find the first non repeating char in a string?
//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);
//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