Remove all duplicates from the input string.

Below are the different methods to remove duplicates in a string.

METHOD 1 (Use Sorting)

Algorithm:

  1) Sort the elements.
  2) Now in a loop, remove duplicates by comparing the 
      current character with previous character.
  3)  Remove extra characters at the end of the resultant string.


Example:

Input string:  geeksforgeeks
1) Sort the characters
   eeeefggkkosss
2) Remove duplicates
    efgkosgkkosss
3) Remove extra characters
     efgkos

Note that, this method doesn’t keep the original order of the input string. For example, if we are to remove duplicates for geeksforgeeks and keep the order of characters same, then output should be geksfor, but above function returns efgkos. We can modify this method by storing the original order. METHOD 2 keeps the order same.

Implementation:

# include <stdio.h>
# include <stdlib.h>

/* Function to remove duplicates in a sorted array */
char *removeDupsSorted(char *str);

/* Utitlity function to sort array A[] */
void quickSort(char A[], int si, int ei);

/* Function removes duplicate characters from the string
   This function work in-place and fills null characters
   in the extra space left */
char *removeDups(char *str)
{
  int len = strlen(str);
  quickSort(str, 0, len-1);
  return removeDupsSorted(str);
}     

/* Function to remove duplicates in a sorted array */
char *removeDupsSorted(char *str)
{
  int res_ind = 1, ip_ind = 1;

  /* In place removal of duplicate characters*/
  while(*(str + ip_ind))
  {
    if(*(str + ip_ind) != *(str + ip_ind - 1))
    {
      *(str + res_ind) = *(str + ip_ind);
      res_ind++;
    }
    ip_ind++;
  }      

  /* After above step string is stringiittg.
     Removing extra iittg after string*/
  *(str + res_ind) = '\0';
  
  return str;
}

/* Driver program to test removeDups */
int main()
{
  char str[] = "eeeefggkkosss";
  printf("%s", removeDups(str));
  getchar();
  return 0;
}

/* FOLLOWING FUNCTIONS ARE ONLY FOR SORTING
    PURPOSE */
void exchange(char *a, char *b)
{
  char temp;
  temp = *a;
  *a   = *b;
  *b   = temp;
}

int partition(char A[], int si, int ei)
{
  char x = A[ei];
  int i = (si - 1);
  int j;

  for (j = si; j <= ei - 1; j++)
  {
    if(A[j] <= x)
    {
      i++;
      exchange(&A[i], &A[j]);
    }
  }
  exchange (&A[i + 1], &A[ei]);
  return (i + 1);
}

/* Implementation of Quick Sort
A[] --> Array to be sorted
si  --> Starting index
ei  --> Ending index
*/
void quickSort(char A[], int si, int ei)
{
  int pi;    /* Partitioning index */
  if(si < ei)
  {
    pi = partition(A, si, ei);
    quickSort(A, si, pi - 1);
    quickSort(A, pi + 1, ei);
  }
}

Time Complexity: O(nlogn) If we use some nlogn sorting algorithm instead of quicksort.



METHOD 2 (Use Hashing )

Algorithm:

1: Initialize:
    str  =  "test string" /* input string */
    ip_ind =  0          /* index to  keep track of location of next
                             character in input string */
    res_ind  =  0         /* index to  keep track of location of
                            next character in the resultant string */
    bin_hash[0..255] = {0,0, ….} /* Binary hash to see if character is 
                                        already processed or not */
2: Do following for each character *(str + ip_ind) in input string:
              (a) if bin_hash is not set for *(str + ip_ind) then
                   // if program sees the character *(str + ip_ind) first time
                         (i)  Set bin_hash for *(str + ip_ind)
                         (ii)  Move *(str  + ip_ind) to the resultant string.
                              This is done in-place.
                         (iii) res_ind++
              (b) ip_ind++
  /* String obtained after this step is "te sringng" */
3: Remove extra characters at the end of the resultant string.
  /*  String obtained after this step is "te sring" */

Implementation:

# include <stdio.h>
# include <stdlib.h>
# define NO_OF_CHARS 256
# define bool int

/* Function removes duplicate characters from the string
   This function work in-place and fills null characters
   in the extra space left */
char *removeDups(char *str)
{
  bool bin_hash[NO_OF_CHARS] = {0};
  int ip_ind = 0, res_ind = 0; 
  char temp;    

  /* In place removal of duplicate characters*/  
  while(*(str + ip_ind))
  {
    temp = *(str + ip_ind);
    if(bin_hash[temp] == 0)
    {
        bin_hash[temp] = 1;
        *(str + res_ind) = *(str + ip_ind);
        res_ind++;         
    }
    ip_ind++;
  }      

  /* After above step string is stringiittg.
     Removing extra iittg after string*/        
  *(str+res_ind) = '\0';   
  
  return str;
}

/* Driver program to test removeDups */
int main()
{
    char str[] = "geeksforgeeks";
    printf("%s", removeDups(str));
    getchar();
    return 0;
} 

Time Complexity: O(n)

NOTES:
* It is assumed that number of possible characters in input string are 256. NO_OF_CHARS should be changed accordingly.
* calloc is used instead of malloc for memory allocations of counting array (count) to initialize allocated memory to ‘\0′. malloc() followed by memset() could also be used.
* Above algorithm also works for an integer array inputs if range of the integers in array is given. Example problem is to find maximum occurring number in an input array given that the input array contain integers only between 1000 to 1100





  • kinshuk chandra

    if characters lies in the range a…z, then we can use following O(n) method without using extra space :

    public static void removeDuplicates(char[] str) {
    int map = 0;
    for (int i = 0; i < str.length; i++) {
    if ((map & (1 < 0) // duplicate detected
    str[i] = 0;
    else // add unique char as a bit ‘1’ to the map
    map |= 1 << (str[i] – 'a');
    }
    }

    (borrowed from kodeknight)

  • Anurag

    #include

    #include

    #include

    #include

    int i,j;

    void no_rep(char s[50])

    {

    int l=strlen(s);

    int flag[l];

    for(i=0;i<l-1;i++)

    {

    for(j=i+1;j<l;j++)

    if(s[i]==s[j])

    flag[j]=0;

    }

    for(i=0;i<l;i++)

    if(flag[i]!=0)

    cout<<s[i];

    }

    void main()

    {

    clrscr();

    char str[50];

    cout<<"Enter string :";

    gets(str);

    cout<<endl<<"String without repetitions is :";

    no_rep(str);

    getch();

    }

  • Gaurav M

    Java program – in place

    public static void main(String[] args)
    {
    String test=”geeksforgeeks”;
    char[] charArray = test.toCharArray();
    int uniqueIndex=0;
    boolean[] recorder = new boolean[256];
    for(int i =0 ; i<charArray.length;i++)
    {
    if(recorder[charArray[i]])
    {
    //do nothing
    }
    else{
    recorder[charArray[i]]=true;
    charArray[uniqueIndex]=charArray[i];
    uniqueIndex++;
    }
    }
    for(int i =0;i<uniqueIndex;i++)
    {
    System.out.print(charArray[i]);
    }
    }

  • ravi singh

    Remove duplicate char from a c++ string:


    string removeDups(string str)
    {
    map my_map;
    int i=0, len = str.length()-1;
    while(i<=len)
    {
    if(!my_map[str[i]])
    {
    my_map[str[i]] = 1;
    i++ ;
    }
    else
    {
    str.erase(i,1);
    len--;
    }
    }
    return str;
    }

  • Guest

    #include
    #include
    #include

    void remdupstr(char str[])
    {
    int i,n=strlen(str),k=0,j;
    char *str2=(char *)malloc(sizeof(char)*n);
    for(i=0;i<n;)
    {
    str2[k]=str[i];
    j=i+1;
    while(j<n&&str[j]==str[i])
    {
    j++;
    }
    k++;
    i=j;
    }
    str2[k++]='';
    printf("aftr removing the dups the str is %sn",str2);
    }

    int main()
    {
    char str[] = "gggeeeeeeksffforrrrgeeeeeksss";
    remdupstr(str);
    return 0;
    }

  • Avinash Nigam

    Java implementation of this algo is as following

    private static String removeDuplicates(char[] charArray)

    {

    int inputIndex = 0, resiudalIndex = 0;

    Map hash = new HashMap();

    while (inputIndex < charArray.length)

    {

    if (!hash.containsKey(String.valueOf(charArray[inputIndex])))

    {

    hash.put(String.valueOf(charArray[inputIndex]), 1);

    charArray[resiudalIndex] = charArray[inputIndex];

    resiudalIndex++;

    }

    inputIndex++;

    }

    for(int i = resiudalIndex; i < charArray.length; i++)

    {

    charArray[i] = '';

    }

    return String.valueOf(charArray);

    }

  • Mohini

    Following method gives only unique char of the string in O(n+k) time, where k can be the set of unique characters in the string but modifies the resulting string in the end. Do leave your comments if you feel anything can be corrected better.

    #include
    #include
    #include

    using namespace std;

    void RemoveDups(char * str)
    {
    int tail = 0,i,j;
    int found;
    for(i = 0; str[i]!=”;i++)
    {
    j =0; found = 0;
    while(j<tail)
    {
    if(str[i] == str[j])
    {
    found = 1;
    break;
    }
    j++;
    }
    if(tail<i && found == 0)
    str[tail] = str[i];
    if(found == 0)
    tail++;
    }
    for(int i=0;i<tail;i++)
    cout<<str[i];
    }

    int main()
    {
    char str[] = "geeksforgeeks";
    RemoveDups(str);
    return 0;
    }

  • Arvind

    please note: isPresent] need to be isPresent]

  • Arvind

    #include
    #include
    #define MAX 256

    using namespace std;

    void removeDuplicates(string text){
    int isPresent[MAX]={0};
    for(int i=0;i<text.length();i++){
    if(isPresent]==0){
    cout<<text[i];
    isPresent]=1;
    }
    }
    }

    int main(){
    string text="geeksforgeeks";
    removeDuplicates(text);
    return 0;
    }

  • Unique
     
    /* public class RemoveDuplicates {
    	public static void main(String[] args) {
    		String s = "geekdforgeeks";
    		String res = "";
    		int count[] = new int[256];
    		for (int i = 0; i < s.length(); i++) {
    			count[s.charAt(i)]++;
    			if (count[s.charAt(i)] > 0 && count[s.charAt(i)] <= 1)
    				res += s.charAt(i);
    		}
    		System.out.println(res);
    	}
    } */
     
  • kaushik
     
    //kaushik sahu
    
    #include<stdio.h>
    #include<conio.h>
    
    int main () 
    {
        char *str1 = "geeksforgeeks";
        char str2[50];
        int byte[26] = {0};
        int i=0,j=0;
        
        for(i=0;*(str1+i) != '\0';i++)
        {
            if(byte[*(str1+i)-97] == 0)
            {
                byte[*(str1+i) - 97] = 1;
                str2[j] = *(str1+i);
                j++;
            }                  
        } 
        str2[j++] = '\0';
        
        printf("%s",str2);
        
        getch();
        return 0;
    }
     
    • Sahil

      was there any need of second string?? this way, we can save the memory of a string and an integer ‘j’

      #include
      #include

      int main ()
      {
      char *str1 = “geeksforgeeks”;

      int byte[26] = {0};
      int i=0;
      clrscr();
      for(i=0;*(str1+i) != ”;i++)
      {
      if(byte[*(str1+i)-97] == 0)
      {

      byte[*(str1+i) – 97] = 1;
      printf(“%c”,*(str1+i));

      }
      }

      getch();
      return 0;
      }

  • miandfdy

    Please tell time and space complexity of my code

     
    /* Paste your code here (You may delete these lines if not writing code) */
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.LinkedList;
    
    
    
    public class Extthread
    {
    	public static void main(String[] args) 
    	{
    		String s="inputing";char[] c=s.toCharArray();Arrays.sort(c);
    			LinkedList l=new LinkedList();
    				for (int i = 0; i < c.length; i++) 
    				{
    					l.add(c[i]);
    				}System.out.println(l);
    				for (int i = 0; i < l.size(); i++) 
    				{
    					for (int j = 1; j < l.size(); j++) 
    					{
    						if(l.get(i)==l.get(j)){l.remove(j);}
    					}
    				}System.out.println(l);
    	}
    }
    
     
    • Ronak Hingar

      Your space complexity is O(n) because of the linkedlist and time complexity is O(n^2)

  • ROHIT SINGHAL
     
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    	static int stk[150],i,len;
    	char a[150];
    	printf("Enter the string\n");
    	scanf("%s",a);
    	len=strlen(a);
    	for(i=0;i<len;i++)
    	{
    		if(stk[a[i]]==0)
    			stk[a[i]]++;
    	}
    	printf("Output is :");
    	for(i=0;i<len;i++)
    	{
    		if(stk[a[i]]-->0)
    			printf("%c",a[i]);
    	}
    
    	printf("\n");
    	
    return 0;
    }
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • hemanthreddy

    It can be solved easily using bit vector

     
    //
    //
    
    #include "stdio.h"
    #include "malloc.h"
    
    #pragma warning(disable : 4996)	   //to use gets function in //visual studio 2012
    
    int main()
    {
    	char *str=(char *)malloc(20*sizeof(char)),*write,*p;
    	int *bitvector=(int *)calloc(8,sizeof(int));
    	gets(str);
    	write=str;
    	p=str;
    	while(*str)
    	{
    		if(!(bitvector[*str>>5] & (1<<(*str%32)))) 
    		{
    			*write++=*str;
    			bitvector[*str>>5] |= 1<<(*str%32);
    		}
    		str++;
    	}
    	*write='\0';
    	puts(p);
    	return 0;
    }
     
    • yelnatz

      Can you explain the logic when you do this:

       
      (bitvector[*str>>5] & (1<<(*str%32)))
       

      Thanks.

  • abhishek08aug
     
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define NO_OF_CHARS 256
    
    char * remove_duplicates(char * str) {
      int * char_count=(int *)calloc(sizeof(int), NO_OF_CHARS);
      int current_copy_index=0;
      char * temp=str;
      while(*temp!='\0') {
        if(*(char_count+*temp)==0) {
          *(char_count+*temp)=1;
          *(str+current_copy_index)=*temp;
          current_copy_index++;
        }
        temp++;
      }
      *(str+current_copy_index)='\0';
      return str;
    }
    
    int main() {
      char * str=(char *)malloc(sizeof(char)*25);
      strcpy(str, "geeksforgeeks");  
      printf("String after removing duplicates is: %s\n", remove_duplicates(str));
      return 0;
    }
     

    String after removing duplicates is: geksfor

  • itengineer

    package com.rsquares.removechars;

    public class CharacterRemover
    {
    public static int[] asciiChars = new int[256];

    public static void main(String[] args)
    {
    CharacterRemover charRemover = new CharacterRemover();
    charRemover.removeDuplicates("aaabbbccnnmmjjkklloooouuuy");
    }

    public void removeDuplicates(String original)
    {
    setMarker(original);

    for(int i=0; i < original.length(); i++)
    {
    if(asciiChars[original.charAt(i)] == -1)
    {
    asciiChars[original.charAt(i)] = -2;
    System.out.print(original.charAt(i));
    }
    }
    }

    private void setMarker(String input)
    {
    for(int j=0; j<input.length(); j++)
    {
    asciiChars[input.charAt(j)] = -1;
    }
    }

    }

  • PG

    Why are we always allocating a dynamic memory for count array. We can allocate a static array of 256 size. if we allocate dynamic memory everytime we have to free memory to avoid memory leak. so in that way static memory should be allocated. correct me if i am wrong.

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • http://sadf JP
     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • pradeep c.r

    #include
    #include
    int main()
    {
    char a[]=”ssssssssssssshhhhhhh”;
    int i,j,pos,k=0;
    clrscr();
    i=0;
    while(a[i]!=”){
    k++;
    i++;
    }
    for(i=0;i<k;i++){
    for(j=i+1;j<k;j++){
    if(a[i]==a[j]){
    pos=j;
    a[i]=a[pos];
    while(a[pos]!=''){
    a[pos]=a[pos+1];
    pos++;
    }
    k–;
    j–;
    }
    }
    }
    i=0;
    while(i<k){
    printf("%c",a[i]);
    i++;
    }
    return 0;
    }

    output: sh

  • manish sahu

    #include
    #include
    int main()
    {
    char s1[30]=”geeksforgeeks”;
    char s2[30];
    int i,j;
    int *count=(int *)calloc(sizeof(int),256);
    for(i=0;s1[i];i++)
    {
    count[s1[i]]++;
    }
    for(i=0,j=0;s1[i];i++)
    {
    if(count[s1[i]]>0)
    {
    s2[j++]=s1[i];
    count[s1[i]]=0;
    }
    }
    s2[j]=”;
    printf(“%s”,s2);
    getch();
    return 0;
    }

  • dejavu

    1.Make a count array and store the count of the characters in the string.
    2.Iterate through the string and if count>0 the add it in the output string and set the count for that character to 0, so that it doesn’t reappear in the string.

    Implementation:

     
    /* public class RemoveDuplicates {
    
    	int[] count = new int[256];
    	public String charPresent(String s)
    	{
    		String unique = "";
    		//collect the count for characters
                    // present in the string
    		for(int i=0;i<s.length();i++)
    		{
    			count[s.charAt(i)]++;
    		}
    		//iterate through the string and 
                    //if the count for a character>0 
                    //then add it to unique 
    		//and reset the count to 0
    		for(int i=0;i<s.length();i++)
    		{
    			if(count[s.charAt(i)]>0)
    			{
    				unique+= s.charAt(i);
                                    //setting the count to 0
    				count[s.charAt(i)]=0; 
                            }
    		}
    		return unique;
    		
    	}
    	
    	
    	public static void main(String[] args) {
    	RemoveDuplicates rd = new RemoveDuplicates();
    	System.out.println(rd.charPresent("geeksforgeeks"));
    
    	}
    
    }*/
     

    Time Complexity O(n).

  • tuhin

    #include
    #include
    #include
    int count[256];

    void makecount()
    { int i=0;
    for(i=0;i<256;i++)
    {
    count[i]=0;
    }
    }

    void removeduplicates(char *s)
    {
    makecount();
    int i,j=0;
    for(i=0;*(s+i)!='';i++)
    {
    if(count[*(s+i)]==0)
    {
    count[*(s+i)]++;
    *(s+j)=*(s+i);
    j++;
    }
    }
    *(s+j)='';
    }

    void main()
    {
    printf("enter the string\n");
    char *s;
    s = (char*)malloc(sizeof(char));
    gets(s);
    removeduplicates(s);
    puts(s);
    }

  • swarnadeep saha
     
    /* Paste your code here (You may delete these lines if not writing code)
    #include<stdio.h>
    void remove_duplicates(char *c)
    {
        printf("\nthe new string is:");
        int arr[256],i;
        for(i=0;i<256;i++)
        arr[i]=-1;
        for(i=0;c[i]!='\0';i++)
        {
            if(arr]==-1)
            {
                arr]=i;
                printf("%c",c[i]);
            }
        }
    }
    main()
    {
        char str[20];
        printf("\nenter the string:");
        gets(str);
        remove_duplicates(str);
    }
     */
     
  • venky

    #include
    #include
    int main()
    {
    char str[50];
    char res[50];
    scanf(“%s”,str);
    int A[26]={0},i=0,k=0,len;
    len=strlen(str);
    for(i=0;i<len;i++)
    {
    if(A[str[i]-97]<1)
    res[k++]=str[i];
    A[str[i]-97]++;

    }
    res[k]='';
    printf("%s\n",res);

    /* Paste your code here (You may delete these lines if not writing code) */

  • Aravindan
     
    /* #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	char a[100],b[100];
    	int i,j,l,k=0;
    	clrscr();
    	for(i=0;(a[i]=getchar())!='$';i++);
    	a[i]='\0';
    
    	for(i=0,l=0;a[i];i++)
    	{
    	l++;
    	}
    
    	for(i=0;a[i];i++)
    	{
    		for(j=0;j!=l;j++)
    		{
    			if(i!=j)
    			{
    				if(a[i]==a[j])
    				{
    					j++;
    				}
    			}
    			else
    			{
    				b[k]=a[i];
    				k++;
    			}
    		}
    	}
    
    	b[k]='\0';
    	printf("%s",b);
    
    	getch();
    }*/
     
  • sudhansu sekhar nayak
     
    /* Paste your code here (You may delete these lines if not writing code) */
    [/import java.io.*;
    /*
    Enter String is :sudhansu
    it remove su
    print sudhan
    */
    import java.util.*;
    class RemoveDuplicate{
       public static void main(String[] args){
    		try	{
    		DataInputStream in = new DataInputStream(System.in);
    		System.out.print(&quot;n n Enter the Sentence : &quot;);
    		String s = in.readLine();
    		boolean flag = false;
    		char c[] = s.toCharArray();
    	    Set set = new HashSet();
    		for(int i = 0 ; i &amp;lt; c.length; i++){
    			  set.add(c[i]); 
    			  }
    			Iterator it = set.iterator();
    			System.out.print(&amp;quot;n n The String is : &amp;quot;);
    			while(it.hasNext())	{
    
                  System.out.print(it.next());}
    		
    		}
    		catch(Exception e){
    
    		}
    	}
    }
     
  • Arnab Sen Gupta
     
    int main()
    {
       char str[] = "geeksforgeeks";
       bool *a = (bool *)calloc(sizeof(bool), NO_OF_CHARS);
       int len = strlen(str);
       char *b = (char *)calloc(sizeof(char),len); //max size possible when all letters occur once
       int i=0,k=0;
       for(i=0;i<len;i++)
       {
                         if(a[str[i]] == 0)
                         {
                                      b[k]=str[i];
                                      a[str[i]] = 1;
                                      k++;
                         }
       }
       printf("%s",b);
       getchar();
       return 0;
    }
     
    • Arnab Sen Gupta

      I wrote the calloc syntax wrong!! they should be as follows–

       
      bool *a = (bool *)calloc(NO_OF_CHARS,sizeof(bool));
      char *b = (char *)calloc(len,sizeof(char));
       
    • arun

      Good one ..

  • Naveen Makwana
     
    int main()
    {
    	char * p="";
    	printf("enter the string :");
    	gets(p);
    	int i,j=1,k=1;
    	for(i=0;p[i] ;i++){
    		while(p[j]){
    			if(p[i]!=p[j]){
    				p[i+k]=p[j];
    				k++;
    			}
    			j++;
    		}
    		p[i+k]='\0';
    		j=i+1;
    		k=1;
    	}
    	printf(p);
    return 0;	 

    }

  • aditya

    #include

    int main()
    {
    char str[]=”aaaabbbccdbdbcd”,str1[30];
    int flag[127]={0};
    int i,rem,j;
    for(i=0,j=0;str[i]!=”;i++)
    {
    rem=str[i]%’A';
    if(flag[rem]==0)
    {
    str1[j++]=str[i];
    flag[rem]=1;
    }
    }
    str1[j]=”;
    printf(“%s\n”,str1);
    }

      
  • intel

    can anyone tell me whats the use of the 3rd step in both the methods.. isn’t it redundant??

  • sujay

    I found this solution online

    public void removeDups(String str) {
    // get the char array
    char[] chArr = str.toCharArray();
    System.out.println(chArr);
    int tail = 1;
    for(int i =0; i< chArr.length; i++)
    {
    int j;
    for(j =0;j< tail; j++) {
    if (chArr[i] == chArr[j])
    break; // break if we find duplicate.
    }
    // if j reachs tail..we did not break, which implies this char at pos i
    // is not a duplicate. So we need to add it our "unique char list"
    // we add it to the end, that is at pos tail.
    if (j == tail) {
    chArr[tail] = chArr[i]; // add
    tail++; // increment tail…[0,tail) is still "unique char list"
    }
    }
    for(; tail < chArr.length;tail++){
    chArr[tail] = 0;
    }
    System.out.println(chArr);
    }

  • picka

    we can use count array tech,
    1. form a count array in d order of given string
    2.print d indexes of d count array

    Example,Geeks
    a[‘g’]=1
    a[‘e’]=2
    a[‘k’]=1
    a[‘s’]=1

    jus print indexes,,geks

    correct me if am wrong???

    • http://www.linkedin.com/in/ramanawithu Venki

      @picka, although count array works, printing characters corresponds to indices results in changing the order of characters in string.

      Or if the idea is to set up the count array in one pass across the string, and in one more pass over the string, print only those characters corresponding to array value 1. However, it is no better than “METHOD 2″ of the above post.

  • yeskay
     #include<string.h>
    
    typedef struct{
    	unsigned char flag:1;
    }CHARMAP;
    
    CHARMAP chars[26];
    
    int main(){
    
    	char* removeAllDuplicates(char*);
    	char str[]="geeksforgeeks";
    	clrscr();
    	printf("Original String is %s\n",str);
    	printf("Altered String is %s\n",removeAllDuplicates(str));
    getch();
    return 0;
    }
    
    
    char *removeAllDuplicates(char *str){
    
    	int act_ptr=0,alt_ptr=0;
    	while(str[act_ptr]!='\0'){
    		if(chars[ str[act_ptr] - 'a'].flag == 0){
    			chars[ str[act_ptr] - 'a'].flag = 1;
    			str[alt_ptr++] = str[act_ptr];
    		}
    	act_ptr++;
    	}
    	str[alt_ptr] = '\0';
    return str;
    } 
  • rajcools

    res_ind = 0 /* index to keep track of location of next
    character in i/p string */
    ip_ind = 0 /* index to keep track of location of
    next character in the resultant string */

    this is a typo. explanation of ip_end and res_ind has been reversed

    • GeeksforGeeks

      @rajcools: Thanks for pointing out the typo. We have corrected it.

  • divyaC
     
    removeDuplicates(char *str){
    	bool setArray[256]={false};
    	int i=0;
    	while(str){
    		if(!setArray[*str]){
    			setArray[*str]=true;
    			str[i]=*str;
    			i++;
    		}
    		str++;
    	}
    	str[i]='';
    } 
  • Rajendra Kumar Uppal

    1. Sorting takes O(nlogn) time.
    2. Other hashing methods are dependent upon input string size, you are expecting that input string is always going to be <= 256 characters.

    What about using following algorithm, which:
    1. platform or language independent;
    2. O(n) time complexity worst-case;
    3. O(1) space complexity no matter if your input string is a 10kb text file.

    Algorithm:
    Step 1. maintain two temporary pointers at starting char of the input string (accessed through array indexing also), first pointer at first char and second pointer at next char;
    Step 2. take 26 booleans or bits (in of c++), keep setting bits corresponding to the ascii value of the char minus 65 or 97
    Step 3. when you see a bit is already set, then move forward, else copy that char to first pointer.
    Step 4. Repeat until string finishes.

    Rajendra.

  • geeksforgeeks

    @Snehal: You are right. We have added a note for this. Also, we have replaced loop with a single statement. Thanks very much. Keep it up!!

  • Snehal

    Also i didnt understand the purpose of running this loop

    /* After above step string is stringiittg.
    37. Removing extra iittg after string*/
    38. for(i = res_ind; i < ip_ind; i++)
    39. *(str+i) = '';

    Refer char *removeDupsSorted(char *str) this function in your code

    instead we can always do
    str[++res_ind]='' and ignore other chars

  • Snehal

    In the first method of using sorting you loose the original order of the characters in the string.So its not feasible ,u need to restore the order too
    So u need one again array (datastructure)to maintain the order of the string..