Last active
February 23, 2022 15:25
-
-
Save lyf-is-coding/7fb6ac2de9394a8cab08d98b1cff5327 to your computer and use it in GitHub Desktop.
C++ STL Remove all duplicates of the string
This file contains hidden or 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
// Input : aabbccaabb | |
// Output : abc??????? | |
// ^ std::unique return iterator | |
#include <string> | |
#include <algorithm> | |
std::string RemoveDuplicates(std::string str) // aabbccaabb | |
{ | |
std::sort(str.begin(), str.end()); // aaaabbbbcc | |
auto itr = std::unique(str.begin(), str.end()); // abc??????? | |
return std::string(str.begin(), itr); // abc | |
} | |
std::cout << RemoveDuplicates("aabbccaabb"); // abc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment