Python Program to Remove Duplicates from String
''.join(dict.fromkeys(your_string)), which keeps characters in order and removes repeats.Examples
How to Think About It
Algorithm
Code
def remove_duplicates(s): return ''.join(dict.fromkeys(s)) # Example usage input_str = 'banana' print(remove_duplicates(input_str))
Dry Run
Let's trace the string 'banana' through the code
Start with input
s = 'banana'
Create dict from keys
dict.fromkeys('banana') creates {'b': None, 'a': None, 'n': None}
Join keys to string
''.join(...) results in 'ban'
| Character | Seen | Result so far |
|---|---|---|
| b | No | b |
| a | No | ba |
| n | No | ban |
| a | Yes | ban |
| n | Yes | ban |
| a | Yes | ban |
Why This Works
Step 1: Using dict.fromkeys()
The dict.fromkeys() method creates a dictionary with characters as keys, automatically removing duplicates because dictionary keys are unique.
Step 2: Preserving order
Since Python 3.7+, dictionaries keep the order of insertion, so the first occurrence of each character is kept in order.
Step 3: Joining keys back to string
Joining the dictionary keys with ''.join() creates a string with duplicates removed but original order preserved.
Alternative Approaches
def remove_duplicates_loop(s): seen = set() result = [] for char in s: if char not in seen: seen.add(char) result.append(char) return ''.join(result) print(remove_duplicates_loop('banana'))
from collections import OrderedDict def remove_duplicates_ordered(s): return ''.join(OrderedDict.fromkeys(s)) print(remove_duplicates_ordered('banana'))
Complexity: O(n) time, O(n) space
Time Complexity
The program loops through each character once, so the time grows linearly with the string length.
Space Complexity
Extra space is used to store seen characters and the result, which can be up to the size of the input string.
Which Approach is Fastest?
Using dict.fromkeys() is concise and efficient in modern Python, while the loop method is more explicit but slightly longer.
| Approach | Time | Space | Best For |
|---|---|---|---|
| dict.fromkeys() | O(n) | O(n) | Quick and clean code in Python 3.7+ |
| Loop with set | O(n) | O(n) | Explicit control, works in all Python versions |
| OrderedDict | O(n) | O(n) | Older Python versions before 3.7 |
dict.fromkeys() to quickly remove duplicates while keeping order in Python 3.7+.set() alone removes duplicates but does not keep the original order of characters.