Skip to content
Related Articles
Open in App
Not now

Related Articles

Remove all consecutive duplicates from a string using STL in C++

Improve Article
Save Article
  • Last Updated : 26 May, 2019
Improve Article
Save Article

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


Input: Geeks for geeks
Output: Geks for geks

Input: aaaaabbbbbb
Output: ab

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

Below is the implementation of the above approach.

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


Geksforgeks is best
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!