Intersection of Two Arrays
Set Membership
Asked at 12 companies
6 recent
- Criteo
- Bloomberg
- Amazon
- Microsoft
- Meta
- MongoDB
- Two Sigma
- Wix
- Accenture
+2 moreShow fewer
- Yandex
- Zoho
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.ReadHide
Problem Statement
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
""" + PROV + r"""
Input / Output
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.
Code it on LeetCode
There's no editor for this one here yet, so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code
Approach
Pattern Recognition
Set membership — the question is just "which values appear in both arrays?", and a hash set answers "is this value in the other array?" in O(1). Build the set once, then keep every nums1 value that is in it, deduplicated.
Approach
Brute Force — Linear Scan for Each Value, O(n * m)
out = set()
for x in nums1:
for y in nums2:
if x == y: out.add(x)
return list(out)
Correct, but every value in nums1 re-scans all of nums2.
Optimized — Hash Set, O(n + m)
s2 = set(nums2) # everything in nums2, once
return list({x for x in nums1 if x in s2})
Example: nums1 = [1,2,2,1], nums2 = [2,2] — the set is {2}, so only 2 survives, and the set comprehension drops the duplicate → [2].
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Linear scan each | O(n * m) | O(n) | Re-scans nums2 for every value |
| Hash set | O(n + m) | O(m) | One lookup per value, duplicates dropped free |
Solution code
class SolutionBruteForce:
"""O(n * m) time | O(n) space — for each value, linear-scan the other array."""
def intersection(self, nums1: list[int], nums2: list[int]) -> list[int]:
out = set()
for x in nums1:
for y in nums2:
if x == y:
out.add(x)
return list(out)
class Solution:
"""O(n + m) time | O(m) space — hash set of nums2, keep matching values."""
def intersection(self, nums1: list[int], nums2: list[int]) -> list[int]:
s2 = set(nums2)
return list({x for x in nums1 if x in s2})Other ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language