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,並在該函式中建立一個名為 myslice1 的切片 1,並在控制檯上列印它。

  • 步驟 3 − 同樣,在控制檯上建立一個名為 myslice2 的切片 2,並在控制檯上列印它。

  • 步驟 4 − 使用 make 函式建立一個空切片以追加兩個切片之間差異的值。

  • 步驟 5 − 執行一個迴圈,直到切片 1 的範圍,並將 found 變數最初設定為 false。

  • 步驟 6 − 在巢狀形式中執行另一個迴圈,並檢查第一個切片中的元素是否等於第二個切片中的元素。

  • 步驟 7 − 如果為真,則將 found 變數設定為 true 並中斷迴圈,但如果為假,則繼續迴圈。

  • 步驟 8 − 迴圈終止時,檢查 found 是否為 false,將切片 1 的值追加到切片差異中並移動到下一個迭代,但如果沒有相同的值,則意味著差異為 0。

  • 步驟 9 − 使用 fmt.Println() 函式在控制檯上列印切片,其中 ln 表示換行。

示例 1

在這個例子中,我們將看到如何使用巢狀 for 迴圈計算兩個切片之間的差異。這裡的差異是指存在於切片 1 中但在切片 2 中不存在的值。

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)
   difference := make([]int, 0) //create difference slice to store the difference of two slices
   // Iterate over slice1
   for _, val1 := range myslice1 { //nested for loop to check if two values are equal
      found := false
      // Iterate over slice2
      for _, val2 := range myslice2 {
         if val1 == val2 {
            found = true
            break
         }
      }
      if !found {
         difference = append(difference, val1)
      }
   }
   fmt.Println("The difference of two slices is:", difference)
}

輸出

The elements of slice1 are: [10 20 30 40 50]
The elements of slice2 are: [30 40 50 60 70]
The difference of two slices is: [10 20]

示例 2

在這個例子中,我們將使用 Go 語言中的對映計算兩個切片之間的差異。元素以一種方式進行檢查,如果它存在於第二個對映中,則不會將其追加到差異切片中。

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)
   difference := make([]int, 0)
   map1 := make(map[int]bool)
   map2 := make(map[int]bool)
   for _, val := range myslice1 {
      map1[val] = true
   }
   for _, val := range myslice2 {
      map2[val] = true
   }
   for key := range map1 {
      if _, ok := map2[key]; !ok {
         difference = append(difference, key) //if element not present in map2 append elements in difference slice
      }
   }
   fmt.Println("The difference between two slices is:")
   fmt.Println("Difference:", difference) //print difference
}

輸出

The elements of slice1 are: [10 20 30 40 50]
The elements of slice2 are: [30 40 50 60 70]
The difference between two slices is:
Difference: [10 20]

結論

我們使用兩個示例執行了查詢兩個切片之間差異的程式。在第一個示例中,我們使用了巢狀 for 迴圈,在第二個示例中,我們使用了 Go 語言中的對映。

更新於: 2023年2月13日

3K+ 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.