Ad
  • Custom User Avatar

    Building up on the previous comment... Look at: https://wiki.python.org/moin/TimeComplexity

    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).

  • Custom User Avatar

    I know writing this is a great exercise!
    But it is already available in the Python standard library (from fractions import Fraction)