Array intersection returns the elements that appear in both input arrays. The result can be defined as a set of distinct values or as a multiset that preserves counts.
You use it when you need to find overlap between datasets.
Problem
Given arrays and , compute:
Two common variants:
- distinct intersection: each value appears once
- multiset intersection: each value appears times
Algorithm
For distinct intersection, use a set.
intersection_distinct(A, B):
S = set of elements in A
R = empty array
for x in B:
if x in S:
append x to R
remove x from S
return RFor multiset intersection, count occurrences.
intersection_multiset(A, B):
count = map from value to frequency in A
R = empty array
for x in B:
if count[x] > 0:
append x to R
count[x] -= 1
return RExample
Let
and
Distinct intersection:
| step | element | result |
|---|---|---|
| 1 | 3 | [3] |
| 2 | 3 | skip |
| 3 | 8 | [3, 8] |
| 4 | 1 | skip |
Result:
Multiset intersection:
| step | element | count left | result |
|---|---|---|---|
| 1 | 3 | 1 | [3] |
| 2 | 3 | 0 | [3, 3] |
| 3 | 8 | 0 | [3, 3, 8] |
| 4 | 1 | 0 | [3, 3, 8] |
Result:
Correctness
For the distinct version, the set contains elements of . Each time a value from appears in the set, it is added once and removed, preventing duplicates.
For the multiset version, the map tracks how many times each value can still be used. Each match decreases the count, ensuring that the result contains exactly the minimum frequency across both arrays.
Complexity
| variant | time | space |
|---|---|---|
| distinct | ||
| multiset |
When to Use
Array intersection is appropriate when:
- finding common elements between datasets
- comparing records or keys
- computing overlaps
If both arrays are sorted, a two-pointer method can compute the intersection in:
time with:
extra space.
Implementation
def intersection_distinct(a, b):
s = set(a)
result = []
for x in b:
if x in s:
result.append(x)
s.remove(x)
return result
def intersection_multiset(a, b):
from collections import Counter
count = Counter(a)
result = []
for x in b:
if count[x] > 0:
result.append(x)
count[x] -= 1
return resultfunc IntersectionDistinct(a, b []int) []int {
s := make(map[int]struct{})
for _, x := range a {
s[x] = struct{}{}
}
result := []int{}
for _, x := range b {
if _, ok := s[x]; ok {
result = append(result, x)
delete(s, x)
}
}
return result
}
func IntersectionMultiset(a, b []int) []int {
count := make(map[int]int)
for _, x := range a {
count[x]++
}
result := []int{}
for _, x := range b {
if count[x] > 0 {
result = append(result, x)
count[x]--
}
}
return result
}