You may consider this question is prev. answered, but the last version doesn’t work with 2 digits number
Given a list of numbers, find and print the elements that appear in it only once. Such elements should be printed in the order in which they occur in the original list.
so here’s my code (being fixed by other pythoneers)
a = [int(s) for s in input().split()]
sortedLst = sorted(a)
unique = []
uniqueOrd = []
for i in range(len(a) - 2):
if sortedLst[i + 1] != sortedLst[i] and sortedLst[i + 1] != sortedLst[i + 2]:
unique.append(sortedLst[i + 1])
for num in a:
if num in unique:
uniqueOrd.append(num)
print(*uniqueOrd)
but the output does not work with 2 digits number, how can I fix this bug?
>Solution :
You don’t have to labor yourself on finding the uniques from a long list, just consider the nice lib from collections – Counter. It’s faster and more Pythonic.
A = [int(s) for s in input().split()]
sortedLst = sorted(A) # this is just to confirm your inputs..
from collections import Counter
counts = Counter(A)
print(counts)
uniques = [k for k, v in counts.items() if v == 1] # just check the frequency (v)
print(uniques)
Try some inputs like: 2 3 1 4 22 33 44 55 33 22
[2, 3, 1, 4, 44, 55]