Skip to content
Related Articles

Related Articles

Remove duplicates from a string using STL in C++

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 28 May, 2019

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


Input: Geeks for geeks
Output: Gefgkors

Input: aaaaabbbbbb
Output: ab

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

Below is the implementation of the above approach.

#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;
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!