Golang program to check if two slices are equal


In this article, we will see what are the ways to check whether two slices are equal or not with the help of relevant examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas a slice is a dynamic array, meaning 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. Let us learn the concept through examples.

Method 1: Using a built-in Function

In this method, we will use reflect.DeepEqual() function from the reflect package to check if the two slices are equal or not. Let’s go through the algorithm and the code to see how it’s executed.

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 initialize a myslice1 with some values inside it.

  • Step 3 − Similarly create two more slices named myslice2 and myslice3 and add some values to those slice.

  • Step 4 − Print all these slices on the console using the print statement in Golang.

  • Step 5 − Use reflect.DeepEqual() from the reflect package and in the first case apply this function to myslice1 and myslice2 and the result will be printed on the console in the form of a Boolean value that whether these two slices are equal or not.

  • Step 6 − Use the same function with myslice1 and myslice3 and in the same way output is printed on the console

Example

Golang program to check if two slices are equal using a built-in function

package main
import (
	"fmt"
	"reflect"
)
func main() {
	myslice1 := []int{10, 20, 30}  //create slice1
	fmt.Println("The elements of slice1 are:", myslice1)
	myslice2 := []int{10, 20, 30}   //create slice2 
	fmt.Println("The elements of slice2 are:", myslice2)

	myslice3 := []int{40, 50, 60}  //create slice3
	fmt.Println("The elements of slice3 are:", myslice3)

	fmt.Println("Let's check whether the slices are equal or not")
	fmt.Println("Are the slice1 and slice2 equal?")

	fmt.Println(reflect.DeepEqual(myslice1, myslice2)) // true
	fmt.Println("Are the slice1 and slice3 equal?")
	fmt.Println(reflect.DeepEqual(myslice1, myslice3)) // false
}

Output

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [10 20 30]
The elements of slice3 are: [40 50 60]
Let's check whether the slices are equal or not
Are the slice1 and slice2 equal?
true
Are the slice1 and slice3 equal?
false

Method 2: Iterating Over Elements of Slices

In this method, we will see if the two slices are equal or not by iterating over elements of slice. We will compare the elements of slices and find the result. Let us go through the algorithm and the code to see how it is executed.

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 initialize a myslice1 with some values inside it.

  • Step 3 − Similarly create one more slice named myslice2 and add some values in that slice.

  • Step 4 − Print all these slices on the console using print statement in Golang.

  • Step 5 − Create a function slice_equality with parameters myslice1 and myslice2 and the value will be returned to the function will be of type Boolean.

  • Step 6 − Check the condition that if length of myslice1 and myslice2 is not equal return false to the function.

  • Step 7 − Run a loop till the length of myslice1 and compare the elements of both slices.

  • Step 8 − If the elements of both slices are not equal return false but if they are equal return true to the function.

  • Step 9 − The output will be printed using fmt.Println() function where ln means new line.

Example

Golang program to check if two slices are equal by iterating over elements of slices.

package main
import (
	"fmt"
)
func main() {
	myslice1 := []int{10, 20, 30} //create slice1
	fmt.Println("The elements of slice1 are:", myslice1)
	myslice2 := []int{10, 20, 30} //create slice2
	fmt.Println("The elements of slice2 are:", myslice2)

	fmt.Println("Let's check whether the slices are equal or not")
	fmt.Println("Are the slice1 and slice2 equal?")

	fmt.Println(slice_equality(myslice1, myslice2)) // true

}

func slice_equality(myslice1, myslice2 []int) bool {
	if len(myslice1) != len(myslice2) {  //if condition is not satisfied print false
		return false
	}
	for i, element := range myslice1 {  // use for loop to check equality
		if element != myslice2[i] {
			return false
		}
	}
	return true
}

Output

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [10 20 30]
Let's check whether the slices are equal or not
Are the slice1 and slice2 equal?
true

Conclusion

In the above program, we used two examples to check whether two slices are equal. In the first method, we used the function of reflect package, and in the second method; we used for loop to compare the slices and check their equality.

Updated on: 23-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements