Signup/Sign In

Remove all duplicates from a given string in Python

In this tutorial, you will learn to remove all duplicates from a given string in Python. Strings in Python are a sequence of characters wrapped inside single, double, or triple quotes. For a given string we have to remove all the characters which occur more than once in the string. We will follow the order in which the characters appear. For example,

Input: "stringis"

Output: "string"

To solve this problem, there are several different approaches,

  1. using OrderedDict() and fromkeys() function
  2. using OrderedDict() function and set approach

We will look at these approaches separately

Approach 1: using OrderedDict() function

In this approach, we will use the OrderedDict() method from the collections class and fromkeys() in our program.

OrderedDict is a dictionary subclass that remembers the order of the keys that were inserted first. Since there can't be duplicate keys this method will return the string after removing the duplicate characters.

Algorithm

Follow the algorithm to understand the approach better:

Step 1- Import OrderedDict from collections class

Step 2- Define a function that will remove duplicates

Step 3- Declare a string with characters

Step 4- Call function to remove characters in that string

Step 5- Print value returned by the function

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

from collections import OrderedDict

def remove_duplicate(s): 
    return "".join(OrderedDict.fromkeys(s))

# test
s="abcfgbsca"
print(s)
print("After removing duplicates: ",remove_duplicate(s))


abcfgbsca
After removing duplicates: abcfgs

Approach 2: OrderedDict

In this approach, we will convert the string to a set by using the set() method. Then we will declare another string and store characters that are not already in the string. This new string will contain the resultant string.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Import OrderedDict from collections class

Step 2- Define a function that will remove duplicates

Step 3- Create a set of string and store

Step 4- Declare a new empty string

Step 5- Run a loop and add each character in the empty string if it is already not present in the string

Step 6- Print the string

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach.

# remove duplicates in string

from collections import OrderedDict

def remove_duplicate(s):
    string=set(s)
    string="".join(string)
    dup=""
    for i in s:
        if(i in dup):
            pass
        else:
            dup=dup+i
    print("After removing: ",dup)
    
s="stdsrdthw"
print(s)
print(remove_duplicate(s))


stdsrdthw
After removing: stdrhw

Conclusion

In this tutorial, we have discussed two approaches for removing duplicates from a string. We have also discussed how to use methods of the collection class and use them to remove duplicates.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.