Go 語言程式:查詢兩個陣列中的不同元素


在本教程中,我們將學習如何編寫一個 Go 語言程式來查詢兩個陣列中的不同元素。本文將編寫兩個程式。第一個程式將使用字串陣列,第二個程式將使用整數陣列。

演算法

步驟 1 − 匯入 fmt 包。

步驟 2 − 定義三個名為 intersection()、uniquearr1 和 uniquearr2 的函式。

步驟 3 − intersection() 函式查詢兩個陣列的公共元素,而其他兩個函式則從給定陣列中刪除這些公共元素。

步驟 4 − 所有這些函式都使用 for 迴圈迭代兩個陣列,並檢查一個數組的當前元素是否等於另一個數組的元素。

步驟 5 − 啟動 main() 函式。

步驟 6 − 初始化兩個字串陣列,並將值儲存到其中。

步驟 7 − 呼叫 intersection() 函式,並將最終結果儲存在不同的變數中。

步驟 8 − 現在,透過傳遞陣列和結果陣列作為引數來呼叫 uniquearr1() 和 uniquearr2()。儲存結果並將兩個陣列追加到一個新陣列中。

步驟 9 − 在螢幕上列印結果。

示例 1

以下程式碼演示瞭如何查詢兩個不同字串陣列中的不同元素。

package main
import "fmt"
// function to get common elements
func intersection(arr1, arr2 []string) []string {
   out := []string{}
   bucket := map[string]bool{}
   for _, i := range arr1 {
      for _, j := range arr2 {
         if i == j && !bucket[i] {
            out = append(out, i)
            bucket[i] = true
         } 
      }
   }
   return out
}

// function to remove common elements from first array
func uniquearr1(arr1, result []string) []string {
   index := len(arr1)
   index1 := len(result)
   for i := 0; i <= index-1; i++ {
      for j := 0; j <= index1-1; j++ {
         if arr1[i] == result[j] {
            arr1[i] = arr1[index-1]
            arr1[index-1] = ""
            arr1 = arr1[:index-1]
            index = index - 1
            i = 0
         }
      }
   }
   return arr1
}
// function to remove common elements from second array
func uniquearr2(arr2, result []string) []string {
   index1 := len(result)
   lenarr2 := len(arr2)
   for i := 0; i <= lenarr2-1; i++ {
      for j := 0; j <= index1-1; j++ {
         if arr2[i] == result[j] {
            arr2[i] = arr2[lenarr2-1]
            arr2[lenarr2-1] = ""
            arr2 = arr2[:lenarr2-1]
            lenarr2 = lenarr2 - 1
            i = 0
         }
      }
   }
   return arr2
}
func main() {
   arr1 := []string{"apple", "mango", "banana", "papaya"}
   fmt.Println("The first array entered is:", arr1)
   arr2 := []string{"cherry", "papaya", "mango"}
   fmt.Println("The second array entered is:", arr2)
   result := intersection(arr1, arr2)
   fmt.Println()
   result1 := uniquearr1(arr1, result)
   result2 := uniquearr2(arr2, result)
   var finalres []string
   finalres = append(finalres, result1...)
   finalres = append(finalres, result2...)
   fmt.Println("The final array containing distinct values from the above mentioned arrays is:", finalres)
}

輸出

The first array entered is: [apple mango banana papaya]
The second array entered is: [cherry papaya mango]

The final array containing distinct values from the above mentioned arrays is: [apple banana cherry]

示例 2

以下程式碼演示瞭如何在 Go 程式語言中查詢兩個不同整數陣列中的不同元素。

package main
import "fmt"

// function to get common elements
func intersection(arr1, arr2 []int) []int {
   out := []int{}
   bucket := map[int]bool{}
   for _, i := range arr1 {
      for _, j := range arr2 {
         if i == j && !bucket[i] {
            out = append(out, i)
            bucket[i] = true
         }
      }
   }
   return out
}
func uniquearr1(arr1, result []int) []int {
   index := len(arr1)
   index1 := len(result)
   for i := 0; i <= index-1; i++ {
      for j := 0; j <= index1-1; j++ {
         if arr1[i] == result[j] {
            arr1[i] = arr1[index-1]
            arr1[index-1] = 0
            arr1 = arr1[:index-1]
            index = index - 1
            i = 0
         }
      }
   }
   return arr1
}
func uniquearr2(arr2, result []int) []int {
   index1 := len(result)
   lenarr2 := len(arr2)
   for i := 0; i <= lenarr2-1; i++ {
      for j := 0; j <= index1-1; j++ {
         if arr2[i] == result[j] {
            arr2[i] = arr2[lenarr2-1]
            arr2[lenarr2-1] = 0
            arr2 = arr2[:lenarr2-1]
            lenarr2 = lenarr2 - 1
            i = 0
         }
      }
   }
   return arr2
}
func main() {
   arr1 := []int{11, 25, 35, 23, 54}
   fmt.Println("The first array entered is:", arr1)
   arr2 := []int{35, 89, 60, 54, 23}
   fmt.Println("The second array entered is:", arr2)
   result := intersection(arr1, arr2)
   fmt.Println()
   result1 := uniquearr1(arr1, result)
   result2 := uniquearr2(arr2, result)
   var finalres []int
   finalres = append(finalres, result1...)
   finalres = append(finalres, result2...)
   fmt.Println("The final array containing distinct values from the above mentioned arrays is:", finalres)
}

輸出

The first array entered is: [11 25 35 23 54]
The second array entered is: [35 89 60 54 23]

The final array containing distinct values from the above mentioned arrays is: [11 25 60 89]

結論

我們已經成功編譯並執行了一個 Go 語言程式來查詢兩個陣列的不同元素,並附帶示例。

更新於: 2023年2月9日

998 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.