Movatterモバイル変換


[0]ホーム

URL:


Open In App

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.

Python
s="geeksforgeeks"res="".join(dict.fromkeys(s))print(res)

Output
geksfor

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.

Python
fromcollectionsimportOrderedDicts="geeksforgeeks"res="".join(OrderedDict.fromkeys(s))print(res)

Output
geksfor

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.

Python
s="geeksforgeeks"seen=set()res=""forcharins:ifcharnotinseen:seen.add(char)res+=charprint(res)

Output
geksfor

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.

Python
s="geeksforgeeks"res="".join([charfori,charinenumerate(s)ifcharnotins[:i]])print(res)

Output
geksfor

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])

S

Shashank Mishra
Improve

S

Shashank Mishra
Improve
Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp