-3

I have a string like 'AABA'. I want to remove multiple occurances by removing others. The result should be 'AB'. Sample Input: AABA Sample Output: AB

Abid1174
  • 123
  • 1
  • 8

1 Answers1

0

If the order doesn't matter, use a set.

word = "AABA"
new_word = "".join(set(word)) 

If the order DOES matter, use an Ordered Dictionary (from collections library).

from collections import OrderedDict

word = "AABA"
new_word = "".join(OrderedDict.fromkeys(word)) 

EDIT: Consult the link posted in the comments above - it gives the same advice, but explains it better.

Andrew Bowling
  • 206
  • 2
  • 8