-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicatechar.cpp
More file actions
73 lines (59 loc) · 1.39 KB
/
Copy pathduplicatechar.cpp
File metadata and controls
73 lines (59 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Time complexity O(n^2)
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
void removeDuplicate1(char *str){//using hashmap
map<char,char> m;
char temp[1];
for(int i=0;i<strlen(str);i++)
m[str[i]]='\0';
strcpy(str," ");
map<char,char>::iterator i;
int j=0;
for(i=m.begin();i!=m.end();i++){
str[j++]=i->first;
}
str[j]='\0';
}
void removeDuplicate2(char *str){//using hashmap
set<char> m;
char temp[1];
for(int i=0;i<strlen(str);i++)
m.insert(str[i]);
set<char>::iterator i;
int j=0;
for(i=m.begin();i!=m.end();i++){
str[j++]=*i;
}
str[j]='\0';
}
void removeDuplicate(char* str){
if(str=='\0')
return;
int pivot=1;
int len=strlen(str);
//i runs from 2nd element to last element because j runs from first element to pivot
//element and we need not compare same elements
for(int i=1;i<len;i++){
int j;
//make all elements before the pivot as unique
for(j=0;j<pivot;j++)
if(str[j]==str[i])
break;
if(j==pivot){
//if j==pivot (str to str+pivot) is a unique subarray where str[pivot]=str[i]
str[pivot]=str[i];
pivot++;
}
}
str[pivot]='\0';
}
int main(){
char str[]="asddfffafffafffafaffafaf";
removeDuplicate2(str);
cout<<str;
}