Go語言程式:查詢兩個陣列的公共元素


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

方法一:使用Append()函式

以下程式碼演示瞭如何使用Go程式語言中的外部函式在兩個不同的字串陣列中查詢公共元素。我們將建立的函式將這兩個陣列作為引數,並返回包含這兩個陣列中公共元素的最終陣列。

演算法

步驟1 − 匯入fmt包。

步驟2 − 定義一個名為intersection()的函式,該函式接受兩個陣列作為引數,並將結果陣列作為函式的輸出返回。

步驟3 − 建立一個名為out的空字串陣列和一個名為bucket的map。

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

步驟5 − 重複上述過程,直到檢查完所有陣列,然後返回最終值。

步驟6 − 開始main()函式。

步驟7 − 初始化兩個字串陣列,並將值儲存到它們中,然後在螢幕上列印這兩個陣列。

步驟8 − 透過將這兩個陣列作為引數傳遞給intersection()函式來呼叫它,並將最終結果儲存在另一個變數中。

步驟9 − 此變數包含具有公共元素的陣列。

步驟10 − 使用fmt.Println()函式在螢幕上列印結果。

示例

使用外部函式查詢兩個字串陣列中公共元素的Go語言程式

package main
import "fmt"

// function to get common elements 2. Golang Program to find the common elements from two arrays
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
}
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()
   fmt.Println("The common elements of the above two arrays are:", result)
}

輸出

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

The common elements of the above two arrays are: [mango papaya]

方法二:使用對映方法

以下程式碼演示瞭如何使用Go程式語言中的外部函式在兩個不同的整數陣列中查詢公共元素。我們將建立的函式將這兩個陣列作為引數,並返回包含這兩個陣列中公共元素的最終陣列。

演算法

步驟1 − 匯入fmt包。

步驟2 − 定義一個名為intersection()的函式,該函式接受兩個陣列作為引數,並將結果陣列作為函式的輸出返回。

步驟3 − 建立一個名為m的map,其鍵為整數,值為布林值。

步驟4 − 使用for迴圈迭代陣列並將它的值儲存到map中。

步驟5 − 現在,使用另一個for迴圈迭代第二個陣列,如果陣列的當前元素等於map中的元素,則將此元素儲存到一個新陣列中。

步驟6 − 重複上述過程,直到檢查完所有陣列,然後返回最終陣列。

步驟7 − 開始main()函式。

步驟8 − 初始化兩個整數陣列,並將值儲存到它們中,然後在螢幕上列印這兩個陣列。

步驟9 − 透過將這兩個陣列作為引數傳遞給intersection()函式來呼叫它,並將獲得的最終結果儲存在另一個變數中。

步驟10 − 此變數包含具有公共元素的陣列。

步驟11 − 使用fmt.Println()函式在螢幕上列印結果。

示例

使用外部函式查詢兩個整數陣列中公共元素的Go語言程式

package main
import (
   "fmt"
)
// creating an Intersection function
func intersection(a, b []int) (c []int) {
   m := make(map[int]bool)
   for _, item := range a {
   
      // storing value to the map
      m[item] = true
   }
   for _, item := range b {
      if _, ok := m[item]; ok {
         c = append(c, item)
      }
   }
   return c
}
func main() {
   arr1 := []int{10, 25, 34, 56, 69}
   fmt.Println("The first array entered is:", arr1)
   arr2 := []int{34, 69, 54, 44}
   fmt.Println("The second array entered is:", arr2)
   fmt.Println()
   result := intersection(arr1, arr2)
   fmt.Println("The common elements of the above two arrays are:", result)
}

輸出

The first array entered is: [10 25 34 56 69]
The second array entered is: [34 69 54 44]

The common elements of the above two arrays are: [34 69]

結論

我們已經成功編譯並執行了一個Go語言程式,用於查詢兩個陣列的公共元素以及示例。我們在這個程式中編寫了兩個程式。第一個程式使用字串陣列,而第二個程式使用整數陣列來實現結果。

更新於:2023年2月9日

1K+ 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

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