# 349. Intersection of Two Arrays

Reference: LeetCode
Difficulty: Easy

## Problem

Given two arrays, write a function to compute their intersection.

Note:

• Each element in the result must be unique.
• The result can be in any order.

Example:

Follow-Up: If it is sorted, can we do it without extra space?

## Analysis

### Brute-Force

Use one set for the result list to avoid duplicates.

Time: $O(M \times N)$
Space: $O(M + N)$

### Use Extra Sets

Solution:

Time: $O(M + N)$
Space: $O(M + N)$

### Built-In Intersection

Time: $O(N + M)$
Space: $O(N + M)$

### Two Pointers If Sorted

Note: Be careful of the conditions.

Comment
Junhao Wang
a software engineering cat