Skip to content

Instantly share code, notes, and snippets.

@lyf-is-coding
Last active February 23, 2022 15:25
Show Gist options
  • Save lyf-is-coding/7fb6ac2de9394a8cab08d98b1cff5327 to your computer and use it in GitHub Desktop.
Save lyf-is-coding/7fb6ac2de9394a8cab08d98b1cff5327 to your computer and use it in GitHub Desktop.
C++ STL Remove all duplicates of the string
// 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