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