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