Open In App

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

Last Updated : 01 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  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 <bits/stdc++.h>
#include <string>
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




<?php
// PHP implementation of above approach
 
// Function to remove duplicates
function removeDuplicatesFromString($str)
{
 
    // keeps track of visited characters
    $counter = 0;
 
    $i = 0;
    $size = strlen($str);
 
    // gets character value
    $x = 0;
 
    // keeps track of length of resultant string
    $length = 0;
 
    while ($i < $size)
    {
        $x = ord($str[$i]) - 97;
 
        // check if Xth bit of counter is unset
        if (($counter & (1 << $x)) == 0)
        {
            $str[$length] = chr(97 + $x);
 
            // mark current character as visited
            $counter = $counter | (1 << $x);
 
            $length++;
        }
        $i++;
    }
 
    return substr($str, 0, $length);
}
 
// Driver code
$str = "geeksforgeeks";
echo removeDuplicatesFromString($str);
 
// This code is contributed by mits
?>


Javascript




<script>
// Javascript implementation of above approach
 
// Function to remove duplicates
function removeDuplicatesFromString(string)
{
    // keeps track of visited characters
        let counter = 0;
        let str = string.split("");
        let i = 0;
        let size = str.length;
  
        // gets character value
        let x;
  
        // keeps track of length of resultant String
        let length = 0;
  
        while (i < size) {
            x = str[i].charCodeAt(0) - 97;
  
            // check if Xth bit of counter is unset
            if ((counter & (1 << x)) == 0) {
  
                str[length] = String.fromCharCode('a'.charCodeAt(0) + x);
  
                // mark current character as visited
                counter = counter | (1 << x);
  
                length++;
            }
            i++;
        }
  
        return str.join("").slice(0,length);
}
 
// Driver code
let  str = "geeksforgeeks";
document.write(removeDuplicatesFromString(str));
 
 
// This code is contributed by patel2127
</script>


Output

geksfor

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: 

  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: 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Method to remove duplicates
string removeDuplicatesFromString(string str)
{
     
    // Table to keep track of visited characters
    vector<int> table(256, 0);
    vector<char> chars;
     
    for(auto i : str)
        chars.push_back(i);
     
    // To keep track of end index of
    // resultant string
    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;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
     
    cout << (removeDuplicatesFromString(str))
         << endl;
}
 
// This code is contributed by Mohit kumar 29


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


Python3




# Python3 implementation of above approach
 
# Method to remove duplicates
def removeDuplicatesFromString(string):
     
    # Table to keep track of visited
    # characters
    table = [0 for i in range(256)]
     
    # To keep track of end index
    # of resultant string
    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
 
# Driver code
if __name__ == '__main__':
     
    temp = "geeksforgeeks"
     
    print(removeDuplicatesFromString(temp))
     
# This code is contributed by Kuldeep 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[256];
        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


Javascript




<script>
 
// JavaScript implementation of above approach
 
    // Method to remove duplicates
    function removeDuplicatesFromString(string)
    {
        // table to keep track of visited characters
        let table = new Array(256);
        for(let i=0;i<table.length;i++)
            table[i]=0;
        let chars = string.split("");
  
        // to keep track of end index of resultant string
        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;
    }
 
    // Driver code
    let str = "geeksforgeeks";
    document.write(removeDuplicatesFromString(str));
     
 
// This code is contributed by unknown2108
 
</script>


Output

geksfor

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:

  1. Iterate over the string letter by letter.
  2. 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.
  3. 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++




// C++ program to create a string of only unique characters
#include <bits/stdc++.h>
using namespace std;
  
// Function to make the string unique
string removeDuplicates(string s)
{
 
    // loop to traverse the string and
    //and check if the first occurrence of
    //each letter matches the current index
    for(int i = 0; i < s.length(); i++)
    {
        // if the first occurrence of s[i]
        //is not i, then remove it from s
        if (s.find(s[i]) != i)
        {
            s.erase(i, 1);
            i--;
        }
    }
    return s;
}
  
// Driver code
int main()
{
      
    // Input string with repeating chars
    string s = "geeksforgeeks";
  
    cout << removeDuplicates(s) << endl;
}
 
//This code is contributed by phasing17


Java




// Java program to create a string of only unique characters
import java.util.*;
 
class GFG {
 
  // Function to make the string unique
  static String removeDuplicates(String s)
  {
 
    // loop to traverse the string and
    // and check if the first occurrence of
    // each letter matches the current index
    for (int i = 0; i < s.length(); i++) {
      // if the first occurrence of s[i]
      // is not i, then remove it from s
      if (s.indexOf(s.charAt(i)) != i) {
        StringBuilder sb = new StringBuilder(s);
        sb.deleteCharAt(i);
        s = sb.toString();
        i--;
      }
    }
    return s;
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Input string with repeating chars
    String s = "geeksforgeeks";
 
    System.out.println(removeDuplicates(s));
  }
}
 
// This code is contributed by phasing17


Python3




# Python3 program to create a string of only unique
# characters
 
# Function to make the string unique
def removeDuplicates(s):
 
    n = len(s)
     
    # loop to traverse the string and
    # and check if the first occurrence of
    # each letter matches the current index
    i = 0
    while i < n:
 
        # if the first occurrence of s[i]
        # is not i, then remove it from s
        if (s.index(s[i]) != i):
            s = s[:i] + s[i + 1:]
            i -= 1
            n -= 1
        else:
            i += 1
 
    return s
 
# Driver code
 
# Input string with repeating chars
s = "geeksforgeeks"
 
# Function Call
print(removeDuplicates(s))
 
# This code is contributed by phasing17


C#




// C# program to create a string of only unique characters
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to make the string unique
  static string removeDuplicates(string s)
  {
 
    // loop to traverse the string and
    // and check if the first occurrence of
    // each letter matches the current index
    for (int i = 0; i < s.Length; i++) {
      // if the first occurrence of s[i]
      // is not i, then remove it from s
      if (s.IndexOf(s[i]) != i) {
        s = s.Remove(i, 1);
        i--;
      }
    }
    return s;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
 
    // Input string with repeating chars
    string s = "geeksforgeeks";
 
    Console.WriteLine(removeDuplicates(s));
  }
}
 
// This code is contributed by phasing17


Javascript




// JavaScript program to create a string of only unique
// characters
 
// Function to make the string unique
function removeDuplicates(s)
{
   
    // loop to traverse the string and
    // and check if the first occurrence of
    // each letter matches the current index
    for (var i = 0; i < s.length; i++) {
       
        // if the first occurrence of s[i]
        // is not i, then remove it from s
        if (s.indexOf(s[i]) != i) {
            s = s.slice(0, i) + s.slice(i + 1, s.length);
            i--;
        }
    }
    return s;
}
 
// Driver code
 
// Input string with repeating chars
let s = "geeksforgeeks";
 
// Function Call
console.log(removeDuplicates(s));
 
// This code is contributed by phasing17


Output

geksfor

Complexity Analysis:

  • Time Complexity: O(N2
  • Auxiliary Space: O(1)


Similar Reads

Duplicates in an array in O(n) time and by using O(1) extra space | Set-3
Given an array of n elements which contains elements from 0 to n-1, with any of these numbers appearing any number of times. Find these repeating numbers in O(n) and using only constant memory space. It is required that the order in which elements repeat should be maintained. If there is no repeating element present then print -1. Examples: Input :
10 min read
Duplicates in an array in O(n) and by using O(1) extra space | Set-2
Given an array of n elements containing elements from 0 to n-1, with any of these numbers appearing any number of times, find these repeating numbers in O(n) and using only constant memory space. Example: Input: n = 7 , array = {1, 2, 3, 1, 3, 6, 6} Output: 1, 3 and 6. Explanation: Duplicate element in the array are 1 , 3 and 6 Input: n = 6, array
7 min read
Find duplicates in O(n) time and O(1) extra space | Set 1
Given an array of n elements that contains elements from 0 to n-1, with any of these numbers appearing any number of times. Find these repeating numbers in O(n) and use only constant memory space. Note: The repeating element should be printed only once. Example: Input: n=7 , array[]={1, 2, 3, 6, 3, 6, 1}Output: 1, 3, 6Explanation: The numbers 1 , 3
6 min read
Check if the characters in a string form a Palindrome in O(1) extra space
Given string str. The string may contain lower-case letters, special characters, digits, or even white spaces. The task is to check whether only the letters present in the string are forming a Palindromic combination or not without using any extra space. Note: It is not allowed to use extra space to solve this problem. Also, the letters present in
10 min read
Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
Given a string representing a number, we need to get the sum of all possible sub strings of this string. Examples : Input : s = "6759" Output : 8421 sum = 6 + 7 + 5 + 9 + 67 + 75 + 59 + 675 + 759 + 6759 = 8421 Input : s = "16" Output : 23 sum = 1 + 6 + 16 = 23Recommended PracticeSum of all substrings of a numberTry It! We have discussed a solution
7 min read
Replace all occurrences of string AB with C without using extra space
Given a string str that may contain one more occurrences of "AB". Replace all occurrences of "AB" with "C" in str. Examples: Input : str = "helloABworld"Output : str = "helloCworld"Input : str = "fghABsdfABysu"Output : str = "fghCsdfCysu"A simple solution is to find all occurrences of "AB". For every occurrence, replace it with C and move all chara
10 min read
Convert Binary Tree to Circular Doubly Linked List using Linear extra space
Given a Binary Tree, convert it to a Circular Doubly Linked List. The left and right pointers in nodes are to be used as previous and next pointers, respectively, in the converted Circular Linked List.The order of nodes in the List must be the same as in the order of the given Binary Tree.The first node of Inorder traversal must be the head node of
12 min read
k smallest elements in same order using O(1) extra space
We are given an array of n-elements you have to find k smallest elements from the array but they must be in the same order as they are in the given array and we are allowed to use only O(1) extra space. Examples: Input : arr[] = {4, 2, 6, 1, 5}, k = 3 Output : 4 2 1 Explanation : 1, 2 and 4 are three smallest numbers and 4 2 1 is their order in giv
8 min read
Shuffle 2n integers as a1-b1-a2-b2-a3-b3-..bn without using extra space
We have an array of the form {a0, a1, a2....., an, b0, b1, b2, .....bn} and our task is to rearrange the same in theform given below by using O(1) space- {a0, b0, a1, b1, a2, b2...........an, bn} Examples: Input : arr[] = {1, 3, 5, 7, 2, 4, 6, 8} Output : {1, 2, 3, 4, 5, 6, 7, 8} Input : arr[] = {11, 13, 15, 12, 14, 16} Output : {11, 12, 13, 14, 15
11 min read
K'th Largest element in BST using constant extra space
Given a binary search tree, task is to find Kth largest element in the binary search tree. Example: Input : k = 3 Root of following BST 10 / \ 4 20 / / \ 2 15 40 Output : 15 The idea is to use Reverse Morris Traversal which is based on Threaded Binary Trees. Threaded binary trees use the NULL pointers to store the successor and predecessor informat
10 min read
Article Tags :
Practice Tags :