# Removing consecutive duplicates from a string

You are given a string. You have to **remove all consecutive duplicate characters and print the resultant string in the end**.

Submitted by Ritik Aggarwal, on January 08, 2019

**Constraints:**

(length of string) < 10000

**Example:**

Sample Input 1: bbccbb Sample Output 1: After removing consecutive duplicates, the answer is :: bcb Sample Input 2: aabccbba Sample Output 2: After removing consecutive duplicates, the answer is :: abcba

**Explanation of the problem:**

- Find the length of the input string and create an empty string to the answer and add the first character of the input string to the answer string.
- Store the 0th character of the string in one character variable (let's name it as first) to store the duplicate character that has already added to the answer string.
- Start iterating from the first index to the end of the input string.
- If the current character is different from stored duplicate variable then add it to our answer string and make the current character as a duplicate character.
- After the whole string has been iterated, return the answer string.

**The time complexity of the above code is O(length of string).**

**C++ Implementation **

#include <iostream> #include <string> using namespace std; string removeduplicates(string s){ int n = s.length(); string ans = ""; // Adding the first character to the ans ans = ans + s[0]; // first is the character to keep the track of included character char first = s[0]; for(int i = 1;i<n;i++){ // ch is the current character char ch = s[i]; /* if already included character is different from our current character then add current character to the ans and assign current character to included character */ if(ch != first){ ans = ans + s[i]; first = s[i]; } } return ans; } // Driver programm to check the code int main() { string s1 ; cout<<"Enter string: "; cin >>s1; cout<<"Entered string is: "<<s1<<endl; cout<<"After removing consecutive duplicates, string: " << removeduplicates(s1) << endl; }

**Output**

Enter string: bbccbb Entered string is: bbccbb After removing consecutive duplicates, string: bcb

Related Tutorials

- Introduction to Algorithms
- Introduction to Greedy Strategy in Algorithms
- Stability in sorting
- External Merge Sorting Algorithm
- Radix Sort and its Algorithm
- Bucket Sort Algorithm
- Bubble sort Algorithm, Flow Chart and C++ Code
- Insertion sort Algorithm, flowchart and C, C++ Code
- Merge Sort | One of the best sorting algorithms used for large inputs
- Binary Search in C, C++
- Randomized Binary Search
- Meta Binary Search | One-sided Binary Search
- Difference between Linear Search and Binary Search
- Binary Search in String
- Variants of Binary Search
- Sieve of Eratosthenes to find prime numbers
- Optimal Merge Pattern (Algorithm and Example)
- Given an array of n numbers, Check whether there is any duplicate or not
- Finding the missing number
- Find the number occurring an odd number of times
- Find the pair whose sum is closest to zero in minimum time complexity
- Find three elements in an array such that their sum is equal to given element K
- Bitonic Search Algorithm
- Check whether a number is Fibonacci or not
- Segregate even and odd numbers in minimum time complexity
- Find trailing zeros in factorial of a number
- Find Nearest Greatest Neighbours of each element in an array
- Interpolation search algorithm
- Floor and ceil of an element in an array using C++
- Two Elements whose sum is closest to zero
- Find a pair with a given difference
- Count number of occurrences (or frequency) in a sorted array
- Find a Fixed Point (Value equal to index) in a given array
- Find the maximum element in an array which is first increasing and then decreasing
- Dynamic Programming (Components, Applications and Elements)
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Find the Nth Fibonacci number | C++
- Longest Common Subsequence using Dynamic programming (DP)
- Longest Increasing Subsequence using Dynamic programming (DP)
- Find the maximum sub-array sum using KADANE'S ALGORITHM
- Non-intersecting chords using Dynamic Programming (DP)
- Edit Distance using Dynamic Programming (DP)
- Finding Ugly Number using Dynamic Programming (DP)

- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Find the GCD (Greatest Common Divisor) of two numbers using EUCLID'S ALGORITHM
- Compute the value of A raise to the power B using Fast Exponentiation
- Implement First Come First Served (FCFS) CPU Scheduling Algorithm using C program
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Analysis of LRU page replacement algorithm and Belady's anomaly
- Branch and Bound
- Find the roots of a complex polynomial equation using Regula Falsi Method in C
- Divide and Conquer Paradigm (What it is, Its Applications, Pros and Cons)
- Strassen's Matrix Multiplication in algorithms
- Huffman Coding (Algorithm, Example and Time complexity)
- Tournament Tree and their properties
- Deterministic and Non Deterministic Algorithms
- Lower Bound Theory
- Non Recursive Tree Traversal Algorithm
- Line Drawing Algorithm
- P and NP problems and solutions | Algorithms
- 2 – 3 Trees Algorithm
- Midpoint Circle Algorithm
- Reliability design problem
- Fast Exponentiation using Bitmasking
- Egg dropping problem using Dynamic Programming (DP)
- Wild card matching problem using Dynamic programming (DP)
- Compute sum of digits in all numbers from 1 to N for a given N
- Minimum jumps required using Dynamic programming (DP)
- Graph coloring problem's solution using backtracking algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- Travelling Salesman Problem
- Kruskal's (P) and Prim's (K) Algorithms
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code

Comments and Discussions!