Go语言程序计算两个切片的对称差
切片类似于数组,唯一的区别是数组是元素的固定序列,而切片的数组元素是动态的。这使得切片在各种应用中更加高效和快速。在切片中,元素是通过引用而不是值传递的。在本文中,我们将学习使用 Go 语言计算两个切片之间对称差的不同技术。
算法
步骤 1 − 创建一个名为 main 的包,并在程序中声明 fmt(格式化包),其中 main 生成可执行代码,fmt 帮助格式化输入和输出。
步骤 2 − 创建一个 main 函数,并在该函数中创建两个切片 slice1 和 slice2,并使用 Go 语言中的打印语句打印这些切片。
步骤 3 − 从 main 函数调用对称差函数,整个程序将在辅助函数内执行。
步骤 4 − 在对称差函数中创建一个 diff 切片,元素将被追加到其中。
步骤 5 − 迭代循环直到 slice1 的范围,并将 found 的初始值设置为 false。
步骤 6 − 迭代第二个切片直到 slice2 的范围,并检查 slice1 的元素是否等于 slice2 的元素,将 found 设置为 true 并中断循环。
步骤 7 − 如果未找到元素,则将 slice1 的元素追加到 diff 切片中。
步骤 8 − 迭代循环直到 slice2 的范围,并将 found 的初始值设置为 false。
步骤 9 − 迭代第一个切片直到 slice1 的范围,并检查 slice2 的元素是否等于 slice1 的元素,将 found 设置为 true 并中断循环。
步骤 10 − 如果未找到元素,则将 slice2 的元素追加到 diff 切片中。
步骤 11 − 将 diff 切片返回到函数,并使用 fmt.Println() 函数在控制台上打印它,其中 ln 表示换行。
示例 1
在本例中,我们将学习如何使用两次 for 循环分别在 slice1 和 slice2 上计算两个切片之间的对称差,然后追加两个切片中不共有的元素。
package main
import "fmt"
func main() {
// create slice1
myslice1 := []int{10, 20, 30, 40, 50}
fmt.Println("The elements of slice1 are:", myslice1)
// create slice2
myslice2 := []int{30, 40, 50, 60, 70}
fmt.Println("The elements of slice2 are:", myslice2)
// to find symmetric differance
diff := symmetricdifference(myslice1, myslice2)
fmt.Println("The symmetric difference of two slices is:")
fmt.Println(diff)
}
func symmetricdifference(myslice1, myslice2 []int) []int {
var diff []int
// to iterate on the element of first number slice
for _, val1 := range myslice1 {
found := false
// Iterate over elements in second slice
for _, elem2 := range myslice2 {
if val1 == elem2 {
found = true
break
}
}
if !found {
diff = append(diff, val1)
}
}
// to iterate on the elements present in slice number 2
for _, elem2 := range myslice2 {
found := false
for _, elem1 := range myslice1 {
if elem2 == elem1 {
found = true
break
}
}
if !found {
diff = append(diff, elem2)
}
}
// to return the symmetric difference
return diff
}
输出
The elements of slice1 are: [10 20 30 40 50] The elements of slice2 are: [30 40 50 60 70] The symmetric difference of two slices is: [10 20 60 70]
示例 2
在本例中,我们将了解如何使用 Go 语言中使用 make 函数构建的 map 计算两个切片之间的对称差,make 函数是一个内置函数。
package main
import (
"fmt"
)
func main() {
myslice1 := []int{10, 20, 30, 40, 50} //create slice1
fmt.Println("The elements of slice1 are:", myslice1)
myslice2 := []int{30, 40, 50, 60, 70} //create slice2
fmt.Println("The elements of slice2 are:", myslice2)
diff := symmetricdifference(myslice1, myslice2)
fmt.Println("The symmetric difference between two slices are:")
fmt.Println(diff)
}
func symmetricdifference(myslice1, myslice2 []int) []int {
// Create a map to store the elements of the first slice
map_store := make(map[int]bool)
for _, val := range myslice1 {
map_store[val] = true
}
var diff []int
// Iterate over elements in second slice
for _, elem := range myslice2 {
if _, ok := map_store[elem]; !ok {
diff = append(diff, elem)
} else {
delete(map_store, elem)
}
}
// Add remaining keys in the map
for key := range map_store {
diff = append(diff, key)
}
return diff //return symmetric difference
}
输出
The elements of slice1 are: [10 20 30 40 50] The elements of slice2 are: [30 40 50 60 70] The symmetric difference between two slices are: [60 70 10 20]
结论
我们使用两个示例执行了计算两个切片之间对称差的程序。在第一个示例中,我们对切片使用了两次 for 循环,在第二个示例中,我们使用 map 存储值并查找对称差。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP