How Remove Duplicate Letters In A String
Is there a way to remove duplicate characters? For example if we input 'hello', the output will be 'helo'; another example is 'overflow', the output would be 'overflw'; another exa
Solution 1:
Change string
to mystring
:
def removeDupes(mystring):
newStr = ""
for ch in mystring:
if ch not in newStr:
newStr = newStr + ch
return newStr
print removeDupes("hello")
print removeDupes("overflow")
print removeDupes("paragraphs")
>>>
helo
overflw
parghs
Solution 2:
yes with something called a set:
unique = set()
[ unique.add(c) for c in 'stringstring' ]
Solution 3:
I would use collections.OrderedDict
for this:
>>> from collections import OrderedDict
>>> data = "paragraphs"
>>> print "".join(OrderedDict.fromkeys(data))
parghs
Post a Comment for "How Remove Duplicate Letters In A String"