← 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 {
    // Micropattern: sort both, then sweep one pointer per array.
}
func intersection(nums1 []int, nums2 []int) []int {
    // Micropattern: sort both, then sweep one pointer per array.
    sort.Ints(nums1)
    sort.Ints(nums2)
}
func intersection(nums1 []int, nums2 []int) []int {
    // Micropattern: sort both, then sweep one pointer per array.
    sort.Ints(nums1)
    sort.Ints(nums2)

    res := []int{}
    i, j := 0, 0
}
func intersection(nums1 []int, nums2 []int) []int {
    // Micropattern: sort both, then sweep one pointer per array.
    sort.Ints(nums1)
    sort.Ints(nums2)

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

    for i < len(nums1) && j < len(nums2) {

    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Micropattern: sort both, then sweep one pointer per array.
    sort.Ints(nums1)
    sort.Ints(nums2)

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

    for i < len(nums1) && j < len(nums2) {
        switch {
        case nums1[i] < nums2[j]:
            i++ // advance the smaller side
        case nums1[i] > nums2[j]:
            j++
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Micropattern: sort both, then sweep one pointer per array.
    sort.Ints(nums1)
    sort.Ints(nums2)

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

    for i < len(nums1) && j < len(nums2) {
        switch {
        case nums1[i] < nums2[j]:
            i++ // advance the smaller side
        case nums1[i] > nums2[j]:
            j++
            // Equal: record once, skip duplicates on both sides.
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Micropattern: sort both, then sweep one pointer per array.
    sort.Ints(nums1)
    sort.Ints(nums2)

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

    for i < len(nums1) && j < len(nums2) {
        switch {
        case nums1[i] < nums2[j]:
            i++ // advance the smaller side
        case nums1[i] > nums2[j]:
            j++
        default:
            // Equal: record once, skip duplicates on both sides.
            if len(res) == 0 || res[len(res)-1] != nums1[i] {
                res = append(res, nums1[i])
            }
            i++
            j++
        }
    }
}
func intersection(nums1 []int, nums2 []int) []int {
    // Micropattern: sort both, then sweep one pointer per array.
    sort.Ints(nums1)
    sort.Ints(nums2)

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

    for i < len(nums1) && j < len(nums2) {
        switch {
        case nums1[i] < nums2[j]:
            i++ // advance the smaller side
        case nums1[i] > nums2[j]:
            j++
        default:
            // Equal: record once, skip duplicates on both sides.
            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.