def dedupe(s):
seen = set()
result = []
for ch in s:
if ch not in seen:
seen.add(ch)
result.append(ch)
return "".join(result)
for s in ["programming", "banana", "aabbcc"]:
print(s, "->", dedupe(s), "|", "".join(dict.fromkeys(s)))
Output Verified by running it on Python 3
programming -> progamin | progamin
banana -> ban | ban
aabbcc -> abc | abc
A set remembers what has been seen; the list keeps the order. dict.fromkeys(s) is a one-line equivalent because dicts keep insertion order.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!