ARTICLE AD BOX
I am trying to find the intersection of two arrays in Python.
My current implementation works, but it is O(n²) because I am checking each element of list a inside list b.
This approach becomes very slow when the input lists are large.
I want to know:
How can I reduce the time complexity to O(n)?
Is there a better data-structure-based approach (like sets or hashing)?
How would the optimized version look in Python?
I’m looking for an efficient and Pythonic way to solve this.
def intersect(a, b): result = [] for x in a: if x in b: result.append(x) return result