DSAHashingSet membership

Array Intersection Visualizer

What is Array Intersection

The intersection is the set of values that appear in both arrays. Put the first array into a hash set, then walk the second array and keep a value only when the set says yes.

  • Membership, not position: we care that a value exists, not where.
  • Dedup the answer: a second set (or the same check) stops repeating 2, 2, 2.
  • Swap if needed: hashing the smaller array uses less memory.

How this visualizer works

A = 1, 2, 2, 1, B = 2, 2. After A is in the set, both 2s from B hit, and the result set is just {2}.

Interactive trace

hash_table.state

Input
Hash table
Current look-up
Match / window
Stored entry

current_action

Load an input, then press Play or Step.

live_trace.py

1def intersection(a, b):
2seen = set(a)
3out = set()
4for x in b:
5if x in seen:
6out.add(x)
7return list(out)

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def intersection(a, b):
2seen = set(a)
3out = set()
4for x in b:
5if x in seen:
6out.add(x)
7return list(out)

complexity.md

OperationTime
Build set(A)O(n)
Scan BO(m)
SpaceO(n)

Python implementation

solution.py

1def intersection(a, b):
2 seen = set(a)
3 out = set()
4 for x in b:
5 if x in seen:
6 out.add(x)
7 return list(out)