Skip to content

Array Intersection

Compute the common elements between two arrays.

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 AA and BB, compute:

AB A \cap B

Two common variants:

  • distinct intersection: each value appears once
  • multiset intersection: each value appears min(countA,countB)\min(count_A, count_B) 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 R

For 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 R

Example

Let

A=[8,3,5,3] A = [8, 3, 5, 3]

and

B=[3,3,8,1] B = [3, 3, 8, 1]

Distinct intersection:

stepelementresult
13[3]
23skip
38[3, 8]
41skip

Result:

[3,8] [3, 8]

Multiset intersection:

stepelementcount leftresult
131[3]
230[3, 3]
380[3, 3, 8]
410[3, 3, 8]

Result:

[3,3,8] [3, 3, 8]

Correctness

For the distinct version, the set contains elements of AA. Each time a value from BB 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

varianttimespace
distinctO(n+m)O(n + m)O(n)O(n)
multisetO(n+m)O(n + m)O(n)O(n)

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:

O(n+m) O(n + m)

time with:

O(1) O(1)

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 result
func 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
}