Open In App

Remove duplicates from a string using STL in C++

Last Updated : 28 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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.CPP
#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;
}
Output:
ab

Next Article
Article Tags :
Practice Tags :

Similar Reads