Set intersection

3 snippets across 3 stacks - Interview Prep, JavaScript, Python

Also written as Set intersection

DSAInterview Prep

Intersection of Collections

DSA · Hash Maps & Sets
Syntax
Convert one collection to a set, iterate the other and check membership.
For sorted arrays: use two pointers instead.
Example
// JavaScript
function intersection(arr1, arr2) {
  const set1 = new Set(arr1);
  const result = [];
  const seen = new Set();
  for (const val of arr2) {
    if (set1.has(val) && !seen.has(val)) {
      result.push(val);
      seen.add(val);
    }
  }
  return result;
}

# Python
def intersection(arr1, arr2):
    return list(set(arr1) & set(arr2))

# With duplicates preserved:
def intersect_with_dupes(arr1, arr2):
    from collections import Counter
    c1, c2 = Counter(arr1), Counter(arr2)
    return list((c1 & c2).elements())
Output
intersection([1,2,2,1], [2,2]) → [2]
intersect_with_dupes([1,2,2,1], [2,2]) → [2, 2]

Note Set approach: O(n + m) time, O(min(n,m)) space. Sorted two-pointer approach: O(n log n + m log m) time, O(1) extra space. Ask the interviewer: unique results or preserve duplicates? Are inputs sorted?

JSJavaScript

Set Methods (ES2025)

JS · Modern Features
Syntax
setA.union(setB)
setA.intersection(setB)
setA.difference(setB)
setA.symmetricDifference(setB)
setA.isSubsetOf(setB)
setA.isSupersetOf(setB)
setA.isDisjointFrom(setB)
Example
const frontend = new Set(["js", "css", "html", "ts"]);
const backend = new Set(["js", "python", "go", "ts"]);

console.log(frontend.union(backend));
// Set {"js", "css", "html", "ts", "python", "go"}

console.log(frontend.intersection(backend));
// Set {"js", "ts"}

console.log(frontend.difference(backend));
// Set {"css", "html"}

console.log(frontend.isSubsetOf(backend)); // false

Note ES2025. All methods return new Sets (non-mutating). Works with any iterable argument, not just Sets. Replaces manual loop-based set operations.

PYPython

Set Operations

PY · Tuples & Sets
Syntax
a | b  (union)
a & b  (intersection)
a - b  (difference)
a ^ b  (symmetric difference)
Example
frontend = {"alice", "bob", "carol"}
backend = {"bob", "carol", "dave"}

print(frontend | backend)
print(frontend & backend)
print(frontend - backend)
print(frontend ^ backend)
Output
{'alice', 'bob', 'carol', 'dave'}
{'bob', 'carol'}
{'alice'}
{'alice', 'dave'}

Note Set operations are extremely fast (O(min(len(a), len(b))) for intersection). Use them instead of nested loops for membership checks.

Frequently asked questions

How does Interview Prep handle set intersection?
This task is covered in 3 stacks on this page: Interview Prep, JavaScript, Python. The "Intersection of Collections" snippet in Interview Prep uses `Convert one collection to a set, iterate the other and check membership.`.
Which code does the Interview Prep example use?
The "Intersection of Collections" snippet uses `Convert one collection to a set, iterate the other and check membership.`, from the Hash Maps & Sets section of the Interview Prep cheat sheet.
Which stacks cover "set intersection" on this page?
Interview Prep, JavaScript, Python. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Intersection of Collections": Set approach: O(n + m) time, O(min(n,m)) space. Sorted two-pointer approach: O(n log n + m log m) time, O(1) extra space. Ask the interviewer: unique results or preserve duplicates? Are inputs sorted?