-1

I have written this code to remove repeating numbers from the list and output the max number. However, it is not removing the number which is repeated on the 4th index value. please help.

array = input()
nums = (array.split())
num = [int(i) for i in nums]

n = 0
for j in num:
 for q in num:
    if q == j:
        n += 1
    if n > 1:
        while j in num:
           num.remove(j)
    n = 0
print(num)
print(max(num))
  • 2
    Does this answer your question? [How do you remove duplicates from a list whilst preserving order?](https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order) – Umutambyi Gad Jul 09 '20 at 10:52
  • 2
    You're removing elements from `num` while iterating over it, which doesn't work. – Tom Karzes Jul 09 '20 at 10:53
  • You could use a set if element order wasnt important – Craicerjack Jul 09 '20 at 10:54
  • `code` array = input() nums = (array.split()) num = [int(i) for i in nums] final = [int(i) for i in nums] n = 0 for j in num: for q in num: if q == j: n += 1 if n > 1: while j in final: final.remove(j) n = 0 print(final) if len(final) == 0: print(-1) else: print(max(final)) – M. Ammar Raza Jul 09 '20 at 10:59

1 Answers1

1

pythons build in function set() does this for you.

_list = [1,2,3,4,5,6,7,8,9,1,2]
print(set(_list))

outputs:

[1,2,3,4,5,6,7,8,9]
mama
  • 2,046
  • 1
  • 7
  • 24