x in list has O(n) complexity, while x in set is O(1) [assuming collisions are unlikely].
So, considering array1 and array2 have length n and m, this code using list has complexity O(n * n * m + n * log n) which can be simplified as O(n**2 * m), while using set can reduce it to O(n * m + n * log n).
Building up on the previous comment... Look at: https://wiki.python.org/moin/TimeComplexity
x in list
has O(n) complexity, whilex in set
is O(1) [assuming collisions are unlikely].So, considering
array1
andarray2
have lengthn
andm
, this code usinglist
has complexityO(n * n * m + n * log n)
which can be simplified asO(n**2 * m)
, while usingset
can reduce it toO(n * m + n * log n)
.I know writing this is a great exercise!
But it is already available in the Python standard library (
from fractions import Fraction
)