Skip to content

Instantly share code, notes, and snippets.

@Meraj
Created February 9, 2021 18:30
Show Gist options
  • Save Meraj/2e3f261daa922db147f52ee051e3079b to your computer and use it in GitHub Desktop.
Save Meraj/2e3f261daa922db147f52ee051e3079b to your computer and use it in GitHub Desktop.
remove all Duplicate letters in a string text
/**
* 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