Home  >  Article  >  Backend Development  >  Tips for decrypting Golang array intersection

Tips for decrypting Golang array intersection

王林
王林Original
2024-04-03 17:39:011152browse

In Go language, the methods to find the intersection of arrays are: use the built-in function Intersect, which is suitable for sorted arrays. Use map, suitable for large arrays or arrays with few elements. Custom sorting and binary search for very large arrays. Which method to choose depends on the size of the array and the distribution of elements.

解密 Golang 数组求交集的技巧

Decrypt the techniques for finding the intersection of Go language arrays

In the Go language, an array is an ordered collection that stores elements of the same type. . Performing an intersection operation on arrays can obtain elements that exist simultaneously in two or more arrays. The following introduces several practical techniques for finding intersections in different scenarios.

Built-in functionsIntersect

Go language provides the Intersect function in the sort package, The intersection of two sorted arrays can be found directly. This function receives two sorted arrays as arguments and returns a new array containing the intersection elements.

package main

import (
    "fmt"
    "sort"
)

func main() {
    arr1 := []int{1, 3, 5, 7, 9}
    arr2 := []int{2, 4, 6, 8, 9}

    sort.Ints(arr1)
    sort.Ints(arr2)

    res := sort.Intersect(arr1, arr2)
    fmt.Println(res) // []9
}

For smaller arrays, the Intersect function is an efficient and concise solution.

Use map

For large arrays or arrays with few elements, it is also effective to use map to find the intersection. Methods. By using an array as a key to a map, you can quickly check whether an element in another array is also in the map.

package main

import "fmt"

func main() {
    arr1 := []int{1, 3, 5, 7, 9}
    arr2 := []int{2, 4, 6, 8, 9}

    m := make(map[int]bool)
    for _, v := range arr1 {
        m[v] = true
    }

    var res []int
    for _, v := range arr2 {
        if m[v] {
            res = append(res, v)
        }
    }

    fmt.Println(res) // []9
}

Custom sorting and binary search

For very large arrays, using custom sorting and binary search algorithms for intersection can achieve better performance. First sort both arrays, then iterate through one of the arrays and perform a binary search on the other to find matching elements.

package main

import (
    "fmt"
    "sort"
)

func main() {
    arr1 := []int{1, 3, 5, 7, 9, 11, 13, 15}
    arr2 := []int{2, 4, 6, 8, 9, 10, 12, 14, 16}

    sort.Ints(arr1)
    sort.Ints(arr2)

    res := intersection(arr1, arr2)
    fmt.Println(res) // []9
}

func intersection(a, b []int) []int {
    var res []int
    for _, v := range a {
        idx := sort.SearchInts(b, v)
        if idx >= 0 && b[idx] == v {
            res = append(res, v)
        }
    }
    return res
}

Based on the array size and element distribution, choosing the most appropriate intersection technique can optimize code performance.

The above is the detailed content of Tips for decrypting Golang array intersection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn