Golang Program to Calculate the intersection of two Slices


In Golang, a slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value.

Syntax

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

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

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

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a function main and in that function create a slice1 and slice2, print that slice on the console using print statement in Golang.

  • Step 3 − Call the function intersect with two parameters of slices on whose elements intersection is to be done.

  • Step 4 − In the function intersect create an empty slice intersect in which the elements are to be appended.

  • Step 5 − Run a for loop for calculating the intersection of two slices

  • Step 6 − After the loop is terminated return the slice to the function intersect and print it on the console using fmt.Println() function where ln means new line.

Example 1

In this example we will use nested for loop to calculate intersection of two slices. Here in this example, we will check if two elements are equal, they will be appended to the slice created. Let’s go through the algorithm and the code to get a crystal-clear understanding of the concept.

package main
import (
   "fmt"
)
func main() {
   myslice1 := []int{10, 20, 30, 40, 50} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{30, 40, 50, 60, 70} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   intersect := intersect(myslice1, myslice2)
   fmt.Println(intersect)
}
func intersect(slice1, slice2 []int) []int {
   var intersect []int
   for _, element1 := range slice1 {
      for _, element2 := range slice2 {
         if element1 == element2 {
            intersect = append(intersect, element1)
         }
      }
   }
   return intersect //return slice after intersection
}

Output

The elements of slice1 are: [10 20 30 40 50]
The elements of slice2 are: [30 40 50 60 70]
[30 40 50]

Example 2

In this example, we will learn how to calculate the intersection of two slices using map function. We will append those elements in the new slice whose values are greater than 0. Let’s go through the algorithm and the code to get a clear understanding of the concept.

package main
import (
   "fmt"
)
func main() {
   myslice1 := []int{10, 20, 30, 40, 50} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{30, 40, 50, 60, 70} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   intersect := intersect(myslice1, myslice2)
   fmt.Println("The elements of slice after intersection are:")
   fmt.Println(intersect)
}
func intersect(myslice1, myslice2 []int) []int {
   k := make(map[int]int) //create map to store elements
   for _, num := range myslice1 {
      k[num]++
   }
   var output []int
   for _, num := range myslice2 {
      if k[num] > 0 {
         output = append(output, num)
         k[num]--
      }
   }
   return output //return slice after intersection
}

Output

The elements of slice1 are: [10 20 30 40 50]
The elements of slice2 are: [30 40 50 60 70]
The elements of slice after intersection are:
[30 40 50]

Conclusion

In the above program, we used two examples to calculate intersection of two slices. In the first example w,e used a nested for loop and in the second example we used a map function.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements