So, I had a similar exercise on my IT classes: ‘Print a string without characters appearing more than once (if they appear more than once, remove them)’. I thought that it was easy (and maybe it is), but I have completely no idea how to do that. I can do similar exercises (print all unique characters from a string / remove duplicates etc).
Example:
Input: ‘12345555555678’
Output: ‘1234678’
Advertisement
Answer
You could use collections.Counter()
.
JavaScript
x
6
1
from collections import Counter
2
3
inp = '12345555555678'
4
c = Counter(inp)
5
output = ''.join(k for k, v in c.items() if v == 1) # -> 1234678
6
Simple implementation of Counter
JavaScript
1
4
1
c = {}
2
for char in inp:
3
c[char] = c.get(char, 0) + 1
4