> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/binary-search-and-tree/349.-intersection-of-two-arrays.md).

# 349. Intersection of Two Arrays

\# Easy

{% hint style="info" %}
Key: use set to improve efficiency.
{% endhint %}

{% tabs %}
{% tab title="Python" %}

```python
class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        result = set()
        set1 = set(nums1)
        set2 = set(nums2)
        
        if len(set1)  <  len(set2):
            return self.helper(set1, set2)
        else:
            return self.helper(set2, set1)
        
    def helper(self, set1, set2):
        return [x for x in set1 if x in set2]

```

Time complexity = $$O(m+n)$$ . Space complexity = $$O(n+m)$$ except the result.
{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}
