# Remove duplicates from a string in O(1) extra space

Given a string str of lowercase characters, the task is to remove duplicates and return a resultant string without modifying the order of characters in the original string.

Examples:

```Input: str = "geeksforgeeks"
Output: geksfor

Input: str = "characters"
Output: chartes
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use bits of a counter variable to mark the presence of a character in the string. To mark the presence of ‘a’ set 0th bit as 1, for ‘b’ set 1st bit as 1 and so on. If the corresponding bit of character present in the original string is set to 0, it means it is the first occurrence of that character, hence set its corresponding bit as 1 and keep on including the current character in the resultant string.

Consider the string str = “geeksforgeeks”

• character: ‘g’
x = 6(ascii of g – 97)
6th bit in counter is unset resulting first occurrence of character ‘g’.
str = ‘g’
counter = 00000000000000000000000001000000 // mark 6th bit as visited
length = 1
• character: ‘e’
x = 4(ascii of e – 97)
4th bit in counter is unset resulting in first occurrence of character ‘e’.
str = ‘e’
counter = 00000000000000000000000001010000 //mark 4th bit as visited
length = 2
• character: ‘e’
x = 4(ascii of e – 97)
4th bit in counter is set resulting in duplicate character.
Ignore this character. Move for next character.
counter = 00000000000000000000000001010000 //same as previous
length = 2
• character: ‘k’
x = 10(ascii of k – 97)
10th bit in counter is unset resulting in first occurrence of character ‘k’.
str = ‘k’
counter = 00000000000000000000010001010000 //mark 10th bit as visited
length = 3

Similarly, do the same for all characters.

Resultant string : geksfor(string of length 7 starting from index 0)

Algorithm:

1. Initialize a counter variable (keeps track of the characters visited in string), it is a 32 bit Integer represented as(00000000000000000000000000000000) initially.
2. Consider ‘a’ as 0th bit of counter, ‘b’ as 1st bit of counter, ‘c’ as 2nd bit of counter and so on.
3. Traverse through each character of input string.
4. Get the character’s value, where character’s value(x) = Ascii of character – 97. This will make sure for value of ‘a’ as 0, value of ‘b’ as 1 and so on.
5. Check xth bit of counter.
6. If Xth bit of counter is 0 which means the current character has appeared for the first time, keep the current character at the index “length” of string .
7. Mark the current character visited by setting xth bit of counter.
8. Increment length.
9. Return Substring of size “length” from index 0.

Below is the implementation of above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// Function to remove duplicates ` `string removeDuplicatesFromString(string str) ` `{ ` ` `  `    ``// keeps track of visited characters ` `    ``int` `counter = 0; ` ` `  `    ``int` `i = 0; ` `    ``int` `size = str.size(); ` ` `  `    ``// gets character value ` `    ``int` `x; ` ` `  `    ``// keeps track of length of resultant string ` `    ``int` `length = 0; ` ` `  `    ``while` `(i < size) { ` `        ``x = str[i] - 97; ` ` `  `        ``// check if Xth bit of counter is unset ` `        ``if` `((counter & (1 << x)) == 0) { ` ` `  `            ``str[length] = ``'a'` `+ x; ` ` `  `            ``// mark current character as visited ` `            ``counter = counter | (1 << x); ` ` `  `            ``length++; ` `        ``} ` `        ``i++; ` `    ``} ` ` `  `    ``return` `str.substr(0, length); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeksforgeeks"``; ` `    ``cout << removeDuplicatesFromString(str); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` `import` `java.util.Arrays; ` ` `  `class` `GFG { ` ` `  `    ``// Function to remove duplicates ` `    ``static` `char``[] removeDuplicatesFromString(String string) ` `    ``{ ` ` `  `        ``// keeps track of visited characters ` `        ``int` `counter = ``0``; ` `        ``char``[] str = string.toCharArray(); ` `        ``int` `i = ``0``; ` `        ``int` `size = str.length; ` ` `  `        ``// gets character value ` `        ``int` `x; ` ` `  `        ``// keeps track of length of resultant String ` `        ``int` `length = ``0``; ` ` `  `        ``while` `(i < size) { ` `            ``x = str[i] - ``97``; ` ` `  `            ``// check if Xth bit of counter is unset ` `            ``if` `((counter & (``1` `<< x)) == ``0``) { ` ` `  `                ``str[length] = (``char``)(``'a'` `+ x); ` ` `  `                ``// mark current character as visited ` `                ``counter = counter | (``1` `<< x); ` ` `  `                ``length++; ` `            ``} ` `            ``i++; ` `        ``} ` ` `  `        ``return` `Arrays.copyOfRange(str, ``0``, length); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str = ``"geeksforgeeks"``; ` `        ``System.out.println(removeDuplicatesFromString(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by Mithun Kumar `

## Python3

 `# Python3 implementation of above approach ` ` `  `# Function to remove duplicates ` `def` `removeDuplicatesFromString(str2): ` ` `  `    ``# keeps track of visited characters ` `    ``counter ``=` `0``; ` ` `  `    ``i ``=` `0``; ` `    ``size ``=` `len``(str2); ` `    ``str1 ``=` `list``(str2); ` ` `  `    ``# gets character value ` `    ``x ``=` `0``; ` ` `  `    ``# keeps track of length of resultant string ` `    ``length ``=` `0``; ` ` `  `    ``while` `(i < size): ` `        ``x ``=` `ord``(str1[i]) ``-` `97``; ` ` `  `        ``# check if Xth bit of counter is unset ` `        ``if` `((counter & (``1` `<< x)) ``=``=` `0``): ` `            ``str1[length] ``=` `chr``(``97` `+` `x); ` ` `  `            ``# mark current character as visited ` `            ``counter ``=` `counter | (``1` `<< x); ` ` `  `            ``length ``+``=` `1``; ` `        ``i ``+``=` `1``; ` `         `  `    ``str2``=``''.join(str1); ` `    ``return` `str2[``0``:length]; ` ` `  `# Driver code ` `str1 ``=` `"geeksforgeeks"``; ` `print``(removeDuplicatesFromString(str1)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# implementation of above approach ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function to remove duplicates ` `    ``static` `string` `removeDuplicatesFromString(``string` `string1) ` `    ``{ ` ` `  `        ``// keeps track of visited characters ` `        ``int` `counter = 0; ` `        ``char``[] str = string1.ToCharArray(); ` `        ``int` `i = 0; ` `        ``int` `size = str.Length; ` ` `  `        ``// gets character value ` `        ``int` `x; ` ` `  `        ``// keeps track of length of resultant String ` `        ``int` `length = 0; ` ` `  `        ``while` `(i < size) { ` `            ``x = str[i] - 97; ` ` `  `            ``// check if Xth bit of counter is unset ` `            ``if` `((counter & (1 << x)) == 0) { ` ` `  `                ``str[length] = (``char``)(``'a'` `+ x); ` ` `  `                ``// mark current character as visited ` `                ``counter = counter | (1 << x); ` ` `  `                ``length++; ` `            ``} ` `            ``i++; ` `        ``} ` ` `  `        ``return` `(``new` `string``(str)).Substring(0, length); ` `    ``} ` ` `  `    ``// Driver code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``string` `str = ``"geeksforgeeks"``; ` `        ``Console.WriteLine(removeDuplicatesFromString(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output:

```geksfor
```

Time Complexity: O(n)
Space Complexity: O(1)

Another Approach: This approach keeps track of visited characters from given input string through an integer array of size 256 (All possible characters).

The idea is as follows:

1. Create an integer array of size 256 in order to keep track of all possible characters.
2. Iterate over the input string, and for each character :
3. Lookup into the array with the ASCII value of character as index:
• If value at index is 0, then copy the character into original input array and increase the endIndex also update the value at index as -1.
• Else skip the character.

Below is the implementation of the above approach:

## Java

 `//Java implementation of above approach  ` `import` `java.util.Arrays;  ` ` `  `class` `GFG {  ` ` `  `    ``// Method to remove duplicates  ` `    ``static` `char``[] removeDuplicatesFromString(String string)  ` `    ``{  ` `        ``//table to keep track of visited characters ` `        ``int``[] table = ``new` `int``[``256``]; ` `        ``char``[] chars = string.toCharArray(); ` ` `  `        ``//to keep track of end index of resultant string ` `        ``int` `endIndex = ``0``; ` `     `  `        ``for``(``int` `i = ``0``; i < chars.length; i++) ` `        ``{ ` `            ``if``(table[chars[i]] == ``0``) ` `            ``{ ` `                ``table[chars[i]] = -``1``; ` `                ``chars[endIndex++] = chars[i];  ` `            ``} ` `        ``} ` `     `  `        ``return` `Arrays.copyOfRange(chars, ``0``, endIndex); ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{  ` `        ``String str = ``"geeksforgeeks"``;  ` `        ``System.out.println(removeDuplicatesFromString(str));  ` `    ``}  ` `}  ` `//This code is contributed by Sonu Singh  `

## C#

 `// C# implementation of above approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `    ``// Method to remove duplicates  ` `    ``static` `char``[] removeDuplicatesFromString(String str)  ` `    ``{  ` `        ``// table to keep track of visited characters ` `        ``int``[] table = ``new` `int``; ` `        ``char``[] chars = str.ToCharArray(); ` ` `  `        ``// to keep track of end index  ` `        ``// of resultant string ` `        ``int` `endIndex = 0; ` `     `  `        ``for``(``int` `i = 0; i < chars.Length; i++) ` `        ``{ ` `            ``if``(table[chars[i]] == 0) ` `            ``{ ` `                ``table[chars[i]] = -1; ` `                ``chars[endIndex++] = chars[i];  ` `            ``} ` `        ``} ` `        ``char` `[]newStr = ``new` `char``[endIndex]; ` `        ``Array.Copy(chars, newStr, endIndex); ` `        ``return` `newStr; ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{  ` `        ``String str = ``"geeksforgeeks"``;  ` `        ``Console.WriteLine(removeDuplicatesFromString(str));  ` `    ``}  ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```geksfor
```

Time Complexity: O(n)
Space Complexity: O(1)

This approach is contributed by Sonu Singh.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.