Golang 查找两个数组中的不同元素

Golang 查找两个数组中的不同元素

在本教程中,我们将看到如何编写一个使用Go语言程序来查找两个数组中不同元素的程序。本文我们将编写两个程序。在第一个程序中,我们将使用字符串数组,而在第二个程序中,我们将使用整数数组。

步骤

步骤1 - 导入fmt包。

步骤2 - 定义名为intersection()、uniquearr1和uniquearr2的三个函数。

步骤3 - intersection()函数找到两个数组中的共同元素,而其他两个函数从给定的数组中删除这些共同元素。

步骤4 - 所有这些函数使用循环遍历两个数组,并检查一个数组的当前元素是否等于另一个数组的元素。

步骤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语言程序,用于查找两个数组的不同元素,并附带示例。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程