If you were to find the first non-repeating alphabet in the string, is there any better way to do it?

var str = "aaabbcddeff"

str = str.split("").sort();

for(i=0;i<str.length;i++){
  if(str[i] != str[i-1] && str[i] != str[i+1] ){
  console.log(str[i]);
  break;
  }
}

Based on your string of “aaabbcddeff”, what should the desired output be?

“c” ofcourse!!..

So if the string was “hello”, the requirement is to sort it first to create “ehllo”? And then return “e”? If I was given just the challenge “find the first non-repeating alphabet in a string”, I would take that to mean, the string should not be sorted, but it should return the first letter which is not repeated, so “hello” would return “h”. However, that is why I asked what output you were expecting for output.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums