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 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 语言程序来查找两个数组中不常见的元素,并提供了示例。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP