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.
#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
filter_none
Output:
ab
Recommended Posts:
- Remove all consecutive duplicates from a string using STL in C++
- Remove duplicates from a sorted array using STL in C++
- Remove duplicates from an unsorted array using STL in C++
- Remove duplicates from an array of small primes
- Remove Trailing Zeros From string in C++
- std::remove, std::remove_if in c++
- Remove comments from a given C/C++ program
- list remove() function in C++ STL
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Remove duplicate elements in an Array using STL in C++
- Find the character in first string that is present at minimum index in second string
- Check if a string can become empty by recursively deleting a given sub-string
- Remove first adjacent pairs of similar characters until possible
- Difference between std::remove and vector::erase for vectors
- std::string::append vs std::string::push_back() vs Operator += in C++
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.



