Remove Duplicate Characters, Keep Order

Medium ⏱ 12 min 46% acceptance ★★★★★ 4.7
Write remove_duplicates(s) that returns s with every repeated character removed, keeping only the first occurrence of each character and preserving the original order.

Examples

Example 1
Input
s = 'programming'
Output
'progamin'
Explanation

Repeated letters (the second r, second m, second g) are dropped; first occurrences are kept in order.

Example 2
Input
s = 'aabbcc'
Output
'abc'
Explanation

Only the first a, first b and first c survive.

Constraints

  • 0 <= len(s) <= 10^4

Topics

StringsDeduplication

Companies

MetaLinkedIn

Hints

Hint 1

Track characters already seen in a set.

Hint 2

Append a character to the result only the first time you see it.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.