Remove duplicates from a string in O(1) extra space
Last Updated :
01 Sep, 2022
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
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[0] = ‘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[1] = ‘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[2] = ‘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:
- Initialize a counter variable (keeps track of the characters visited in string), it is a 32 bit Integer represented as(00000000000000000000000000000000) initially.
- Consider ‘a’ as 0th bit of counter, ‘b’ as 1st bit of counter, ‘c’ as 2nd bit of counter and so on.
- Traverse through each character of input string.
- 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.
- Check xth bit of counter.
- 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 .
- Mark the current character visited by setting xth bit of counter.
- Increment length.
- Return Substring of size “length” from index 0.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
#include <string>
using namespace std;
string removeDuplicatesFromString(string str)
{
int counter = 0;
int i = 0;
int size = str.size();
int x;
int length = 0;
while (i < size) {
x = str[i] - 97;
if ((counter & (1 << x)) == 0) {
str[length] = 'a' + x;
counter = counter | (1 << x);
length++;
}
i++;
}
return str.substr(0, length);
}
int main()
{
string str = "geeksforgeeks" ;
cout << removeDuplicatesFromString(str);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static char [] removeDuplicatesFromString(String string)
{
int counter = 0 ;
char [] str = string.toCharArray();
int i = 0 ;
int size = str.length;
int x;
int length = 0 ;
while (i < size) {
x = str[i] - 97 ;
if ((counter & ( 1 << x)) == 0 ) {
str[length] = ( char )( 'a' + x);
counter = counter | ( 1 << x);
length++;
}
i++;
}
return Arrays.copyOfRange(str, 0 , length);
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.println(removeDuplicatesFromString(str));
}
}
|
Python3
def removeDuplicatesFromString(str2):
counter = 0 ;
i = 0 ;
size = len (str2);
str1 = list (str2);
x = 0 ;
length = 0 ;
while (i < size):
x = ord (str1[i]) - 97 ;
if ((counter & ( 1 << x)) = = 0 ):
str1[length] = chr ( 97 + x);
counter = counter | ( 1 << x);
length + = 1 ;
i + = 1 ;
str2 = ''.join(str1);
return str2[ 0 :length];
str1 = "geeksforgeeks" ;
print (removeDuplicatesFromString(str1));
|
C#
using System;
class GFG {
static string removeDuplicatesFromString( string string1)
{
int counter = 0;
char [] str = string1.ToCharArray();
int i = 0;
int size = str.Length;
int x;
int length = 0;
while (i < size) {
x = str[i] - 97;
if ((counter & (1 << x)) == 0) {
str[length] = ( char )( 'a' + x);
counter = counter | (1 << x);
length++;
}
i++;
}
return ( new string (str)).Substring(0, length);
}
static void Main()
{
string str = "geeksforgeeks" ;
Console.WriteLine(removeDuplicatesFromString(str));
}
}
|
PHP
<?php
function removeDuplicatesFromString( $str )
{
$counter = 0;
$i = 0;
$size = strlen ( $str );
$x = 0;
$length = 0;
while ( $i < $size )
{
$x = ord( $str [ $i ]) - 97;
if (( $counter & (1 << $x )) == 0)
{
$str [ $length ] = chr (97 + $x );
$counter = $counter | (1 << $x );
$length ++;
}
$i ++;
}
return substr ( $str , 0, $length );
}
$str = "geeksforgeeks" ;
echo removeDuplicatesFromString( $str );
?>
|
Javascript
<script>
function removeDuplicatesFromString(string)
{
let counter = 0;
let str = string.split( "" );
let i = 0;
let size = str.length;
let x;
let length = 0;
while (i < size) {
x = str[i].charCodeAt(0) - 97;
if ((counter & (1 << x)) == 0) {
str[length] = String.fromCharCode( 'a' .charCodeAt(0) + x);
counter = counter | (1 << x);
length++;
}
i++;
}
return str.join( "" ).slice(0,length);
}
let str = "geeksforgeeks" ;
document.write(removeDuplicatesFromString(str));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Space Complexity: O(n) -> As it uses char[] array of string to store characters of string (i.e. dependent upon length of input string)
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:
- Create an integer array of size 256 in order to keep track of all possible characters.
- Iterate over the input string, and for each character :
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
string removeDuplicatesFromString(string str)
{
vector< int > table(256, 0);
vector< char > chars;
for ( auto i : str)
chars.push_back(i);
int endIndex = 0;
for ( int i = 0; i < chars.size(); i++)
{
if (table[chars[i]] == 0)
{
table[chars[i]] = -1;
chars[endIndex++] = chars[i];
}
}
string ans = "" ;
for ( int i = 0; i < endIndex; i++)
ans += chars[i];
return ans;
}
int main()
{
string str = "geeksforgeeks" ;
cout << (removeDuplicatesFromString(str))
<< endl;
}
|
Java
import java.util.Arrays;
class GFG {
static char [] removeDuplicatesFromString(String string)
{
int [] table = new int [ 256 ];
char [] chars = string.toCharArray();
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);
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.println(removeDuplicatesFromString(str));
}
}
|
Python3
def removeDuplicatesFromString(string):
table = [ 0 for i in range ( 256 )]
endIndex = 0
string = list (string)
for i in range ( len (string)):
if (table[ ord (string[i])] = = 0 ):
table[ ord (string[i])] = - 1
string[endIndex] = string[i]
endIndex + = 1
ans = ""
for i in range (endIndex):
ans + = string[i]
return ans
if __name__ = = '__main__' :
temp = "geeksforgeeks"
print (removeDuplicatesFromString(temp))
|
C#
using System;
class GFG
{
static char [] removeDuplicatesFromString(String str)
{
int [] table = new int [256];
char [] chars = str.ToCharArray();
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;
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
Console.WriteLine(removeDuplicatesFromString(str));
}
}
|
Javascript
<script>
function removeDuplicatesFromString(string)
{
let table = new Array(256);
for (let i=0;i<table.length;i++)
table[i]=0;
let chars = string.split( "" );
let endIndex = 0;
for (let i = 0; i < chars.length; i++)
{
if (table[chars[i].charCodeAt(0)] == 0)
{
table[chars[i].charCodeAt(0)] = -1;
chars[endIndex++] = chars[i];
}
}
let ans= "" ;
for (let i=0;i<endIndex;i++)
ans += chars[i]
return ans;
}
let str = "geeksforgeeks" ;
document.write(removeDuplicatesFromString(str));
</script>
|
Complexity AnaysAnalysisis:
- Time Complexity: O(n)
- Space Complexity: O(n) -> As it uses char[] array of string to store characters of string (i.e. dependent upon length of input string)
This approach is contributed by Sonu Singh.
Another Approach:
In cases where adding space is especially expensive, we can utilize the following approach:
- Iterate over the string letter by letter.
- Find the first occurrence of the letter at the ith position of the string. This can be done using in-built methods such as find in C++ STL, indexOf in Java, index in Python, and indexOf in JavaScript.
- If the first occurrence is not equal to i, then delete the character from the string. This can be done using in-built methods such as erase in C++ STL, and deleteCharAt in Java, or using built-in methods to construct a substring.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string removeDuplicates(string s)
{
for ( int i = 0; i < s.length(); i++)
{
if (s.find(s[i]) != i)
{
s.erase(i, 1);
i--;
}
}
return s;
}
int main()
{
string s = "geeksforgeeks" ;
cout << removeDuplicates(s) << endl;
}
|
Java
import java.util.*;
class GFG {
static String removeDuplicates(String s)
{
for ( int i = 0 ; i < s.length(); i++) {
if (s.indexOf(s.charAt(i)) != i) {
StringBuilder sb = new StringBuilder(s);
sb.deleteCharAt(i);
s = sb.toString();
i--;
}
}
return s;
}
public static void main(String[] args)
{
String s = "geeksforgeeks" ;
System.out.println(removeDuplicates(s));
}
}
|
Python3
def removeDuplicates(s):
n = len (s)
i = 0
while i < n:
if (s.index(s[i]) ! = i):
s = s[:i] + s[i + 1 :]
i - = 1
n - = 1
else :
i + = 1
return s
s = "geeksforgeeks"
print (removeDuplicates(s))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static string removeDuplicates( string s)
{
for ( int i = 0; i < s.Length; i++) {
if (s.IndexOf(s[i]) != i) {
s = s.Remove(i, 1);
i--;
}
}
return s;
}
public static void Main( string [] args)
{
string s = "geeksforgeeks" ;
Console.WriteLine(removeDuplicates(s));
}
}
|
Javascript
function removeDuplicates(s)
{
for ( var i = 0; i < s.length; i++) {
if (s.indexOf(s[i]) != i) {
s = s.slice(0, i) + s.slice(i + 1, s.length);
i--;
}
}
return s;
}
let s = "geeksforgeeks" ;
console.log(removeDuplicates(s));
|
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Similar Reads
Remove extra spaces from a string
Given a string containing many consecutive spaces, trim all spaces so that all words should contain only a single space between them. The conversion should be done in-place and solution should handle trailing and leading spaces and also remove preceding spaces before common punctuation like full sto
14 min read
Remove duplicates from a string
Given a string s which may contain lowercase and uppercase characters. The task is to remove all duplicate characters from the string and find the resultant string. Note: The order of remaining characters in the output should be the same as in the original string. Example: Input: s = geeksforgeeksOu
10 min read
Remove spaces from a given string
Given a string, remove all spaces from the string and return it. Input: "g eeks for ge eeks "Output: "geeksforgeeks" Input: "abc d "Output: "abcd" Expected time complexity is O(n) and only one traversal of string. Below is a Simple Solution 1) Iterate through all characters of given string, do follo
8 min read
C++ Program to remove spaces from a string
Given a string, remove all spaces from it. For example "g e e k" should be converted to "geek" and " g e " should be converted to "ge". The idea is to traverse the string from left to right and ignore spaces while traversing. We need to keep track of two indexes, one for the current character being
2 min read
Character replacement after removing duplicates from a string
Given a string. The task is to replace each character of the minimized string with a character present at the index 'IND' of the original string. The minimized string is the string obtained by removing all duplicates from the original string keeping the order of elements the same. IND for any index
7 min read
Remove "b" and "ac" from a given string
Given a string, eliminate all "b" and "ac" in the string, you have to replace them in-place, and you are only allowed to iterate over the string once. (Source Google Interview Question) Examples: acbac ==> "" aaac ==> aa ababac ==> aa bbbbd ==> dWe strongly recommend that you click here
12 min read
Find one extra character in a string
Given two strings which are of lengths n and n+1. The second string contains all the characters of the first string, but there is one extra character. Your task is to find the extra character in the second string. Examples: Input : string strA = "abcd"; string strB = "cbdae"; Output : e string B con
15+ min read
Remove odd indexed characters from a given string
Given string str of size N, the task is to remove the characters present at odd indices (0-based indexing) of a given string. Examples : Input: str = âabcdefâOutput: aceExplanation:The characters 'b', 'd' and 'f' are present at odd indices, i.e. 1, 3 and 5 respectively. Therefore, they are removed f
4 min read
Reverse a string preserving space positions
Given a string s, the task is to reverse the given string while preserving the position of spaces. Examples: Input: "internship at geeks for geeks"Output: skeegrofsk ee gtapi hsn retniExplanation : Reversing the characters without spaces "skeegrofskeegtapihsnretni" and inserting space at original pl
7 min read
Remove all occurrences of a character from a string using STL
Given a string S and a character C, the task is to remove all the occurrences of the character C from the given string. Examples: Input:vS = "GFG IS FUN", C = 'F' Output:GG IS UN Explanation: Removing all occurrences of the character 'F' modifies S to "GG IS UN". Therefore, the required output is GG
2 min read