Skip to content
Advertisement

how to find multiple non-repeating numbers in python?

I have a below method that finds the non-repeating elements in a list:

def dupes(a):
    s = {}
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    for x in s:
        if s[x] == 1:
            return 'this is the only non-repeating element value is :', s[x], 'and the key is :', x
    return

l = [4, 7, 4, 5, 7, 6, 5, 6, 10]
cd = dupes(l)
print("This is dupes: ", cd)

The code runs successfully and below is the output:

This is dupes:  ('this is the only non-repeating element value is :', 1, 'and the key is :', 10)

But when I am trying to add more than one non-repeating elements in the list, the output doesn’t change. For example, if I add 11 at the end of the list the output still remains the same as above..

Any idea?

Advertisement

Answer

Actually when you return at the 10th line, the function ends. This is in case you don’t understand the list comprehension yet, as many have given you the solution with that technique. I will just give you a simple solution where it will return a list of non-repeating numbers.

def dupes(a):
    s = {}
    non_dupes = []
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    for x in s:
        if s[x] == 1:
            non_dupes.append(x)
    return non_dupes

l = [4, 7, 4, 5, 7, 6, 5, 6, 10, 11]
cd = dupes(l)
print("This is dupes: ", cd)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement