The task is to remove all duplicate characters from a string while keeping the first occurrence of each character in its original order.For example:
Input:"geeksforgeeks"
Output:"geksfor"
Let’s explore multiple methods to remove duplicates from a string in Python.
Using dict.fromkeys()
This method converts the string into a dictionary where each character is a key. Since dictionary keys are unique and insertion order is preserved, it effectively removes duplicates while keeping the original order.
Pythons="geeksforgeeks"res="".join(dict.fromkeys(s))print(res)
Explanation:
- dict.fromkeys(s) creates a dictionary with characters of s as keys, automatically removing duplicates.
- "".join(...) combines the dictionary keys back into a string.
- Efficient because it scans the string once and preserves order.
Using OrderedDict.fromkeys()
OrderedDict works similarly to a dictionary but is explicitly designed to preserve order. It ensures duplicates are removed while keeping the insertion sequence intact.
PythonfromcollectionsimportOrderedDicts="geeksforgeeks"res="".join(OrderedDict.fromkeys(s))print(res)
Explanation:
- OrderedDict.fromkeys(s) creates an ordered dictionary of unique characters.
- "".join(...) converts the keys into a string in the original order.
Using for Loop with a Set
This approach removes duplicates by iterating through the string with afor loop, tracking previously seen characters using a set. The first occurrence of each character is added to the result string, which preserves the order.
Pythons="geeksforgeeks"seen=set()res=""forcharins:ifcharnotinseen:seen.add(char)res+=charprint(res)
Explanation:
- seen stores characters that have already appeared.
- The loop adds a character to res only if it hasn’t been seen.
Using List Comprehension with Slicing
This method uses list comprehension and checks each character against the substring before it to remove duplicates. While functional, it is slower for large strings due to repeated slicing.
Pythons="geeksforgeeks"res="".join([charfori,charinenumerate(s)ifcharnotins[:i]])print(res)
Explanation:
- enumerate(s) gives the index and character.
- char not in s[:i] ensures only the first occurrence of each character is added.
- Less efficient due to repeated slicing (s[:i])
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice