Resolved: Replace sequence of characters without RegExp in JS

In this post, we will see how to resolve Replace sequence of characters without RegExp in JS


I am trying to replace a sequence of characters from string without regular expressions.
All sequances must be replaced by the char only 1 time and if the same char is matched later it should stay in the result as well.
Any help without RegExp will be much appreciated.
I’ve tried with .replace and .substring but I am hitting a hard rock with both because when the chars at the end are matched it replaces the ones at the beginig.

Best Answer:

Simple approach would be to iterate through each char in the string, then checking if the current character is the same as the last character, and appending the current character to the output string only if it is different from the last character.
So The resulting output string contains only the distinct characters from the input string in the order of their first occurrence:

If you have better answer, please add a comment about this, thank you!