Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript First non repeated character

unicorn

New Coder
JavaScript:
var t = "oops";

var res;

var ar = t.split('');


for(let i =0; i< ar.length ; i++){

    var c =0;

    for(let j=0; j< ar.length ; j++){

        if(ar === ar[j]){

            c += 1;

        }

    }

    if(c < 2){

         res = ar;

        break;

    }

}

console.log(res);

Can someone tell me why c is set to 0 inside for loop but not before for loop?
 
Last edited:
So my understanding is that you loop over each character and then check it against every other character in the sequence.
When you check a new character, you need to reset 0 such that the counting can begin again.

eg.
for the letter "o" - c is set to 0, then it loops through each letter and increments c. At the end c is greater than or equal to 2 so the first loop continues.
the same is repeated for the second "o" (setting c to 0 again)
on the third character - c is set to 0 again and counting can begin, using the 2nd loop to look through the word again. This time c is less than 2 so it can return the result.


I used to find that if you sit with a pen and paper and work through the algorithm with the values you expect you will get a better understanding into the reason why. (Eventually for simple algorithms, you'll basically be able to do this in your head)
 
So my understanding is that you loop over each character and then check it against every other character in the sequence.
When you check a new character, you need to reset 0 such that the counting can begin again.

eg.
for the letter "o" - c is set to 0, then it loops through each letter and increments c. At the end c is greater than or equal to 2 so the first loop continues.
the same is repeated for the second "o" (setting c to 0 again)
on the third character - c is set to 0 again and counting can begin, using the 2nd loop to look through the word again. This time c is less than 2 so it can return the result.


I used to find that if you sit with a pen and paper and work through the algorithm with the values you expect you will get a better understanding into the reason why. (Eventually for simple algorithms, you'll basically be able to do this in your head)
thank youu
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom