Created
February 9, 2021 18:30
-
-
Save Meraj/2e3f261daa922db147f52ee051e3079b to your computer and use it in GitHub Desktop.
remove all Duplicate letters in a string text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* remove Duplicate letters in a text | |
* @param textString | |
* @return | |
*/ | |
string removeDuplicates(std::string textString) { | |
if (textString.begin() == textString.end()) return textString; | |
auto no_duplicates = textString.begin(); | |
for (auto current = no_duplicates; current != textString.end();) { | |
current = std::find_if(std::next(current), textString.end(), [no_duplicates](const char c) { return c != *no_duplicates; }); | |
*++no_duplicates = std::move(*current);; | |
} | |
textString.erase(++no_duplicates, textString.end()); | |
return textString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment