Write code to remove the duplicate characters in a string
without using any additional buffer NOTE: One or two additional variables are fine
An extra copy of the array is not.
This question is from the book “cracking thecoding interview”.
Time complexity= O(n^2)
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 |
//www.GauravVichare.com #include<stdio.h> #include<string.h> void dup(char a[]); void main() { int i; char a[30]; printf("Enter string:"); scanf("{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}s",a); dup(a); printf("{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}s",a); } void dup(char a[]) { int i,j,n,flag,k; char temp; n=strlen(a); for(i=0;i<n;i++) { for(j=k=i+1;k<=n;) { if(a[i]!=a[k]) { a[j]=a[k]; j++; k++; } else { k++; } } }//www.GauravVichare.com } |
Leave comment if you have any problem with code.
Recursive insertion sort
Recent Comments