← Back

Intersection of Two Arrays

Two pointers across two sorted arrays, advancing the smaller side.

Easy LeetCode ↗ 1 / 1
func intersection(nums1 []int, nums2 []int) []int {

}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        // Left value is smaller: it can't appear later in the sorted nums2, so
        // it has no match — skip it.
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        // Left value is smaller: it can't appear later in the sorted nums2, so
        // it has no match — skip it.
        case nums1[i] < nums2[j]:
            i++
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        // Left value is smaller: it can't appear later in the sorted nums2, so
        // it has no match — skip it.
        case nums1[i] < nums2[j]:
            i++

        // Symmetric case: skip the smaller value on the right side.
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        // Left value is smaller: it can't appear later in the sorted nums2, so
        // it has no match — skip it.
        case nums1[i] < nums2[j]:
            i++

        // Symmetric case: skip the smaller value on the right side.
        case nums1[i] > nums2[j]:
            j++
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        // Left value is smaller: it can't appear later in the sorted nums2, so
        // it has no match — skip it.
        case nums1[i] < nums2[j]:
            i++

        // Symmetric case: skip the smaller value on the right side.
        case nums1[i] > nums2[j]:
            j++

        // Equal: it's in both. Record it once (the guard skips duplicates, since
        // each array may repeat values), then advance both sides.
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        // Left value is smaller: it can't appear later in the sorted nums2, so
        // it has no match — skip it.
        case nums1[i] < nums2[j]:
            i++

        // Symmetric case: skip the smaller value on the right side.
        case nums1[i] > nums2[j]:
            j++

        // Equal: it's in both. Record it once (the guard skips duplicates, since
        // each array may repeat values), then advance both sides.
        default:
            if len(res) == 0 || res[len(res)-1] != nums1[i] {
                res = append(res, nums1[i])
            }
            i++
            j++
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Sort both, then sweep them together like the merge step of merge sort:
    // comparing the two fronts tells us which side to advance.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0

    // Once either array runs out, no more shared values are possible.
    for i < len(nums1) && j < len(nums2) {
        switch {
        // Left value is smaller: it can't appear later in the sorted nums2, so
        // it has no match — skip it.
        case nums1[i] < nums2[j]:
            i++

        // Symmetric case: skip the smaller value on the right side.
        case nums1[i] > nums2[j]:
            j++

        // Equal: it's in both. Record it once (the guard skips duplicates, since
        // each array may repeat values), then advance both sides.
        default:
            if len(res) == 0 || res[len(res)-1] != nums1[i] {
                res = append(res, nums1[i])
            }
            i++
            j++
        }
    }

    return res
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.