Golang program to calculate difference between two slices


A slice can also be called as dynamic array as it's value is dynamic whereas normal array is static. This makes slice more efficient and faster. They are passed by reference instead by value. Here we are going to learn about how can we find the difference between two slices or two dynamic arrays.

Syntax

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 with name myslice1 and print it on the console.

  • Step 3 − Similarly create a slice2 on the console with name myslice2 and print it on the console.

  • Step 4 − Create an empty slice using make function to append the values of difference between two slices.

  • Step 5 − Run a loop till the range of the slice1 and set found variable initially to false.

  • Step 6 − Run another loop in nested form and check if the elements in the first slice are equal to the elements in the second slice.

  • Step 7 − If its true set the found variable to true and break the loop but if its not true continue the loop.

  • Step 8 − When the loop is terminated check if the found is false append the value of slice1 into the slice difference and move to next iteration but if there is no same value, it implies that the difference is 0.

  • Step 9 − Print the slice on the console using fmt.Println() function where ln means new line.

Example 1

In this example, we will see how the difference between two slices is calculated using nested for loop. The difference here means that the values which are in slice1 but not present in slice2.

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)
   difference := make([]int, 0) //create difference slice to store the difference of two slices
   // Iterate over slice1
   for _, val1 := range myslice1 { //nested for loop to check if two values are equal
      found := false
      // Iterate over slice2
      for _, val2 := range myslice2 {
         if val1 == val2 {
            found = true
            break
         }
      }
      if !found {
         difference = append(difference, val1)
      }
   }
   fmt.Println("The difference of two slices is:", difference)
}

Output

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

Example 2

In this example, we will calculate difference between two slices using maps in Golang. The elements are checked in a way that if its present in second map it will not be appended in the difference slice.

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)
   difference := make([]int, 0)
   map1 := make(map[int]bool)
   map2 := make(map[int]bool)
   for _, val := range myslice1 {
      map1[val] = true
   }
   for _, val := range myslice2 {
      map2[val] = true
   }
   for key := range map1 {
      if _, ok := map2[key]; !ok {
         difference = append(difference, key) //if element not present in map2 append elements in difference slice
      }
   }
   fmt.Println("The difference between two slices is:")
   fmt.Println("Difference:", difference) //print difference
}

Output

The elements of slice1 are: [10 20 30 40 50]
The elements of slice2 are: [30 40 50 60 70]
The difference between two slices is:
Difference: [10 20]

Conclusion

We executed the program of finding difference between two slices using two examples. In the first example we used nested for loop and in the second example we used maps in Golang.

Updated on: 13-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements