-3

Would appreciate if anyone could provide a short method to remove the repeated letters.

Let's say I have a list of strings ['APPLE', 'BANANA']. I want to remove the repeated letters in the list items and the output maintain original order as ['APLE', 'BAN']. These are for n items in the list.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Sumit Sharma
  • 11
  • 1
  • 5

3 Answers3

0

You can use a dictionary / array to store encountered letters in the word and for each such word, prepare a temporary string accordingly and in the end, push it in your result array.
Something like this :

final = []
for word in words:
    s = ''
    seen = {}
    for letter in word:
        if letter in seen:
            pass
        else:
            s += letter
            seen[letter] = True
    final.append(s)
0

This works, a simple oneliner that preserves the order:

>>> a = [ 'APPLE', 'GOOGLE', 'BANANA' ]
>>> [''.join( dict.fromkeys(i).keys() ) for i in a]
['APLE', 'GOLE', 'BAN']
lenik
  • 23,228
  • 4
  • 34
  • 43
0
words = ['APPLE', 'BANANA']
output = []

for word in words:
    seen = ''
    for char in word:
        if char not in seen:
            seen += char
    output.append(seen)
print(output)