Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Try to find unique numbers in a long list, and return them in order. (prev. version did not work)

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading