Golang 在两个数组中查找不常见的元素
在本教程中,我们将编写一个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 uncommon 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 uncommon 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 uncommon 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 uncommon values from the above mentioned arrays is: [11 25 60 89]
结论
我们成功编译和执行了一个Go语言程序来找到两个数组中不常见的元素,并附有示例。