Remove duplicates from a string using STL in C++

Given a string S, remove duplicates in this string using STL in C++

Examples:

Input: Geeks for geeks
Output: Gefgkors

Input: aaaaabbbbbb
Output: ab

Approach:
The consecutive duplicates of the string can be removed using the unique() function provided in STL.

Below is the implementation of the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    string str = "aaaaabbbbbb";
    sort(str.begin(), str.end());
  
    // Using unique() method
    auto res = unique(str.begin(), str.end());
  
    cout << string(str.begin(), res)
         << endl;
}

chevron_right


Output:

ab


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.