Skip to content

Commit 461d95f

Browse files
authored
Merge pull request #813 from siddhi-244/Siddhi/recursion
Added remove duplicates
2 parents 2813e11 + d898609 commit 461d95f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Recursion/remove_duplicates.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*Given a string S, remove consecutive duplicates from it recursively*/
2+
#include <iostream>
3+
#include <string.h>
4+
using namespace std;
5+
void removeduplicates(char input[]){
6+
if(input[0]=='\0'){
7+
return ;//if it is empty string
8+
}
9+
if(input[0]==input[1]){//checking for consecutive duplicate
10+
for(int i=0;input[i]!='\0';i++){
11+
input[i]=input[i+1];//shift to left
12+
}
13+
removeduplicates(input);
14+
}else{
15+
removeduplicates(input+1);
16+
}
17+
}
18+
19+
int main() {
20+
char input[1000];
21+
cin.getline(input,1000);
22+
removeduplicates(input);
23+
cout<<input;
24+
}
25+
/*Example
26+
aaaabbbbb
27+
O/p ab
28+
*/

0 commit comments

Comments
 (0)