-1

This is my code to find values common between 2 arrays.

li1 = [int(x) for x in input().split()]
li2 = [int(h) for h in input().split()]
for m in li1 :
    for j in li2 :
        if m == j :
            print(m, end=" ")
            j = float("-inf")
            break

When I do li1 = [5, 5, 5] and li2 = [5, 5], it returns 5 5 5. I want this to be 5 as li2 only has two 5. How can I do this.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Vtechster
  • 47
  • 3
  • 1
    Use `set`s: [How to find list intersection?](https://stackoverflow.com/questions/3697432/how-to-find-list-intersection) – pault Oct 26 '20 at 17:58

1 Answers1

1

You can do the same more simply by using sets and intersection:

li1 = [1, 2, 3]
li2 = [2, 3, 4]
shared = list(set(li1).intersection(set(li2))

print(shared) # prints [2, 3]
vvvvv
  • 25,404
  • 19
  • 49
  • 81