Skip to main content

83. Intersection of Two Arrays

easyAsked at Reddit

Return the intersection of two arrays as unique values. Reddit uses this hash-set warm-up to test set operations — the same primitive used to find overlapping users between two subreddits for cross-promotion.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen, easy warm-up.

Problem

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.

Constraints

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

Examples

Example 1

Input
nums1 = [1,2,2,1], nums2 = [2,2]
Output
[2]

Example 2

Input
nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output
[9,4]

Approaches

1. Nested loop check

For each element in nums1, check if it's in nums2.

Time
O(n*m)
Space
O(n + m)
// Anti-pattern: quadratic.

Tradeoff: TLE for large inputs.

2. Set intersection (optimal)

Convert nums1 to set; filter nums2 by membership; uniquify result.

Time
O(n + m)
Space
O(n + m)
function intersection(nums1, nums2) {
  const set1 = new Set(nums1);
  const out = new Set();
  for (const x of nums2) if (set1.has(x)) out.add(x);
  return [...out];
}

Tradeoff: Linear, single pass. Uses Set membership for O(1) lookup.

Reddit-specific tips

Reddit interviewers want hash set immediately. Bonus signal: connect to user-set overlap queries between subreddits (a recommendation primitive).

Common mistakes

  • Using Array.includes (O(n) lookup).
  • Forgetting uniqueness in the output.
  • Modifying inputs in place when not allowed.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Intersection of Two Arrays II (LC 350) — multiplicities preserved.
  • Intersection of three arrays.
  • Intersection on sorted arrays (two-pointer).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

What if arrays are sorted?

Two-pointer merge gives O(n + m) with O(1) extra space.

Multiset intersection?

Use Map of counts. LC 350 covers this.

Practice these live with InterviewChamp.AI

Drill Intersection of Two Arrays and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →