Go语言程序计算两个切片的并集
在 Go 语言中,切片被称为动态数组,其值不固定,可以更改。与简单的数组相比,它更有效率且速度更快。在本文中,我们将学习如何使用示例计算两个不同切片的并集。
语法
func make ([] type, size, capacity)
Go 语言中的 make 函数用于创建数组/映射,它接受要创建的变量类型、大小和容量作为参数。
func append(slice, element_1, element_2…, element_N) []T
append 函数用于向数组切片添加值。它接受多个参数。第一个参数是要添加值的数组,后跟要添加的值。然后,该函数返回包含所有值的最终数组切片。
算法
步骤 1 − 创建一个 package main 并声明 fmt(格式包)包在程序中,其中 main 生成可执行代码,fmt 帮助格式化输入和输出。
步骤 2 − 创建一个 main 函数,并在该函数中创建一个 slice1 和 slice2,并在控制台上打印这些切片。
步骤 3 − 调用函数 union_ele,并将 slice1 和 slice2 作为参数,这两个参数将相互组合。
步骤 4 − 创建一个空映射,它将使用 make 函数(一个内置函数)存储并集的元素。
步骤 5 − 创建一个名为 output 的切片,使用 make 函数(一个内置函数)存储元素的并集。
步骤 6 − 运行一个循环,直到 slice1 的范围,并检查每个元素是否存在于循环中,如果存在则不要将其添加到映射中,否则将其添加到映射中,并使用 append 函数将其添加到 output 切片中。
步骤 7 − 同样,对 slice2 重复相同的过程,并使用 append 函数将其添加到 output 切片中。
步骤 8 − 将 output 返回到函数,它将使用 fmt.Println() 函数在控制台上打印,其中 ln 表示换行。
示例 1
在本例中,我们将学习如何使用外部函数计算两个切片的并集。make 和 append 内置函数将在本例中发挥重要作用。让我们一起了解算法和代码以理解这个概念。
package main
import (
"fmt"
)
func union_ele(myslice1, myslice2 []int) []int {
// Create a map to store the elements of the union
values := make(map[int]bool)
for _, key := range myslice1 { // for loop used in slice1 to remove duplicates from the values
values[key] = true
}
for _, key := range myslice2 { // for loop used in slice2 to remove duplicates from the values
values[key] = true
}
// Convert the map keys to a sliceq5
output := make([]int, 0, len(values)) //create slice output
for val := range values {
output = append(output, val) //append values in slice output
}
return output
}
func main() {
myslice1 := []int{10, 20, 30, 40} //create slice1
fmt.Println("The elements of slice1 are:", myslice1)
myslice2 := []int{30, 40, 50, 60} //create slice2
fmt.Println("The elements of slice2 are:", myslice2)
fmt.Println("The union of elements presented here is:")
fmt.Println(union_ele(myslice1, myslice2)) //print union of two slices
}
输出
The elements of slice1 are: [10 20 30 40] The elements of slice2 are: [30 40 50 60] The union of elements presented here is: [60 10 20 30 40 50]
示例 2
在本例中,我们将学习如何使用嵌套循环结合内置函数 make 和 append 函数来计算两个切片的并集。输出将在控制台上打印。让我们看看它是如何执行的。
package main
import (
"fmt"
)
func union_ele(myslice1, myslice2 []int) []int {
elements := make(map[int]bool) //create empty map
output := make([]int, 0) //create output slice
for i := range myslice1 {
elements[myslice1[i]] = true
output = append(output, myslice1[i])
}
for i := range myslice2 {
if _, ok := elements[myslice2[i]]; !ok {
elements[myslice2[i]] = true
output = append(output, myslice2[i])
}
}
return output //return union of two slices
}
func main() {
myslice1 := []int{10, 20, 30, 40} //create slice1
fmt.Println("The elements of slice1 are:", myslice1)
myslice2 := []int{30, 40, 50, 60} //create slice2
fmt.Println("The elements of slice2 are:", myslice2)
fmt.Println("The slice after union of elements is presented as:")
fmt.Println(union_ele(myslice1, myslice2)) //print union of slices
}
输出
The elements of slice1 are: [10 20 30 40] The elements of slice2 are: [30 40 50 60] The slice after union of elements is presented as: [10 20 30 40 50 60]
结论
在上面的程序中,我们使用了两个示例打印两个切片的并集。在第一个示例中,我们使用了 for 循环,在第二个示例中,我们使用了嵌套 for 循环。因此,程序成功执行。
数据结构
网络
关系型数据库
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP