Golang 计算两个切片的并集

Golang 计算两个切片的并集

在Golang中,切片被称为动态数组,其值不固定且可更改。它比简单数组更高效和更快。在本文中,我们将学习如何使用示例计算两个不同切片的并集。

语法

func make ([] type, size, capacity)

在Go语言中,make函数用于创建数组/映射,它接受要创建的变量类型、大小和容量作为参数

func append(slice, element_1, element_2…, element_N) []T

append函数用于将值添加到数组切片中。它接受多个参数。第一个参数是我们希望将值添加到的数组,接着是要添加的值。函数会返回包含所有值的最终切片。

步骤

  • 步骤1 - 创建一个main包,并在程序中声明fmt(格式化包)包,其中的main函数产生可执行代码,fmt帮助格式化输入和输出。

  • 步骤2 - 创建一个main函数,并在该函数中创建slice1和slice2,并将它们打印到控制台上。

  • 步骤3 - 使用slice1和slice2作为参数调用union_ele函数,它们将被组合到一起。

  • 步骤4 - 创建一个空映射,用于存储union的元素,使用make函数创建。

  • 步骤5 - 创建一个名为output的切片,使用make函数创建,用于存储元素的union。

  • 步骤6 - 运行一个循环,直到slice1的范围,并检查每个元素是否存在于循环中,如果存在,则不将其添加到映射中,否则将其添加到映射中,并使用append函数将其添加到output切片中。

  • 步骤7 - 使用相同的过程重复slice2,并使用append函数将其添加到output切片中。

  • 步骤8 - 返回output给函数,并使用fmt.Println()函数打印到控制台上,其中ln表示换行。

示例1

在这个示例中,我们将学习如何使用一个外部函数计算两个切片的union。make和append内置函数在这个示例中将起到重要的作用。让我们通过算法和代码来理解这个概念。

package main
import (
   "fmt"
)
func union_ele(myslice1, myslice2 []int) []int {
   // Create a map to store the elements of the union
   values := make(map[int]bool)
   for _, key := range myslice1 { // for loop used in slice1 to remove duplicates from the values
      values[key] = true
   }
   for _, key := range myslice2 { // for loop used in slice2 to remove duplicates from the values
      values[key] = true
   }
   // Convert the map keys to a sliceq5
   output := make([]int, 0, len(values)) //create slice output
   for val := range values {
      output = append(output, val) //append values in slice output
   }
   return output
}
func main() {
   myslice1 := []int{10, 20, 30, 40} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{30, 40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   fmt.Println("The union of elements presented here is:")
   fmt.Println(union_ele(myslice1, myslice2)) //print union of two slices
}

输出

The elements of slice1 are: [10 20 30 40]
The elements of slice2 are: [30 40 50 60]
The union of elements presented here is:
[60 10 20 30 40 50]

示例2

在这个示例中,我们将学习如何使用嵌套循环来计算使用内置函数make和append函数合并两个切片。输出将打印在控制台上。让我们看看它是如何执行的。

package main
import (
   "fmt"
)
func union_ele(myslice1, myslice2 []int) []int {
   elements := make(map[int]bool) //create empty map
   output := make([]int, 0) //create output slice
   for i := range myslice1 {
      elements[myslice1[i]] = true
      output = append(output, myslice1[i])
   }
   for i := range myslice2 {
      if _, ok := elements[myslice2[i]]; !ok {
         elements[myslice2[i]] = true
         output = append(output, myslice2[i])
      }
   }
   return output //return union of two slices
}
func main() {
   myslice1 := []int{10, 20, 30, 40} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{30, 40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   fmt.Println("The slice after union of elements is presented as:")
   fmt.Println(union_ele(myslice1, myslice2)) //print union of slices
}

输出

The elements of slice1 are: [10 20 30 40]
The elements of slice2 are: [30 40 50 60]
The slice after union of elements is presented as:
[10 20 30 40 50 60]

结论

在上述程序中,我们使用了两个示例来打印两个切片的并集。在第一个示例中,我们使用了一个for循环,在第二个示例中我们使用了嵌套的for循环。因此,程序成功执行。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程