0

writing code for the following algorithmic problem and no idea why it's not working. Following a debugger, I found that the elem variable never iterates beyond 's'. I'm concerned that this could be because of my understanding of how to break out of a parent for loop. I read this question: Best way to break from nested loops in Javascript? but I'm not sure if perhaps I'm doing something wrong.

function firstNonRepeatingLetter(s) {
  //input string
  //return first character that doesn't repeat anywhere else.
  //parent for loop points to the char we are analyzing
  //child for loop iterates over the remainder of the string
  //if child for loop doesnt find a repeat, return the char, else break out of the child for loop and cont

 if (s.length == 1) { return s;}

  parent_loop:
  for (var i = 0; i < s.length - 1; i++){ //parent loop
      var elem = s[i];
      child_loop:
      for (var j = i + 1; j < s.length; j++){
          if (elem == s[j]){
            break child_loop;
          }
      }
    return s[i];
    } 
 return "";
}  

console.log(firstNonRepeatingLetter('stress')); // should output t, getting s.
0

3 Answers 3

1

My suggestion would be to use single for loop instead of using two loops.

for( var i = 0; i<s.length - 1;i++) {
    var lastIndex = s.lastIndexOf(s[i]);
    if ( lastIndex == i) {
       return s[i];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

try with this.

this was the key of my code

if( f!=true)
return s[i];

see the full code

function firstNonRepeatingLetter(s) {
  //input string
  //return first character that doesn't repeat anywhere else.
  //parent for loop points to the char we are analyzing
  //child for loop iterates over the remainder of the string
  //if child for loop doesnt find a repeat, return the char, else break out of the child for loop and cont

 if (s.length == 1) { return s;}


  for (var i = 0; i < s.length - 1; i++){ //parent loop
      var elem = s[i];

    var f=false;
      for (var j = i + 1; j < s.length; j++){

          if (elem == s[j]){

            f=true;
            break;
          }

      }

    if( f!=true)
    return s[i];
    } 

 return "";
}  

console.log(firstNonRepeatingLetter('stress')); // should output t, getting s.

Comments

0

I think you are breaking out the loop correctly, the reason why the function always return s is because the counter i never increments.

Let's run through the code

i = 0, element = 's', j = 1, s[j] = 't', which is not equal to 's'
j++, j = 2, s[j] = 'r', not equal to 's'
j++, j = 3, s[j] = 'e', not equal to 's' 
j++, j = 4, s[j] = 's', equal to 's'

so you break out of the child loop.

Now we hit the line return s[i], when i = 0, so naturally the function returns s.

If you change the return s[i] to include an if statement, like so

if(j == s.length) { 
    return s[i];
}

The function now returns 't'. You are checking to see if the child loop ran to its full completion, which means break child_loop; never ran, and you have an unique letter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.