Golang Program to Check If Two Arrays are Equal or Not


In this tutorial, we will see to write a go language program to check if two arrays are equal or not.

Check If Two Arrays are Equal or Not Using Equality Operator

The following code illustrates how we can check if two arrays are equal or not using the equality operator.

Algorithm

Step 1 − First, we need to import the fmt package that allows us to print anything on the screen.

Step 2 − Then we are calling the main() function.

Step 3 − Then we are initializing three arrays of integer data type as arr1, arr2, arr3 and storing values to them

Step 4 − Print these arrays on the screen using fmt.Println() function.

Step 5 − We are using if conditions to compare any two arrays at a time and checking if they are identical or not.

Step 6 − If the chosen two arrays are identical then we need to print that the chosen two arrays are same otherwise print that the given arrays are not equal.

Step 7 − Check this by comparing all the three arrays one by one.

Example

package main
import "fmt"
func main() {
   arr1 := [4]int{1, 2, 3, 4}
   fmt.Println("The first array Arr1 is:", arr1)
   arr2 := [4]int{5, 6, 7, 8}
   fmt.Println("The second array Arr2 is:", arr2)
   arr3 := [4]int{1, 2, 3, 4}
   fmt.Println("The third array Arr3 is:", arr3)
   fmt.Println()
   if arr1 == arr2 {
      fmt.Println("Arr1 and Arr2 have the similar elements")
   } else {
      fmt.Println("Arr1 and Arr2 do not have the similar elements")
   }
   if arr1 == arr3 {
      fmt.Println("Arr1 and Arr3 have the similar elements")
   } else {
      fmt.Println("Arr1 and Arr3 do not have the similar elements")
   }
   if arr2 != arr3 {
      fmt.Println("Arr2 and Arr3 do not have the similar elements")
   } else {
      fmt.Println("Arr2 and Arr3 have the similar elements")
   }
}

Output

The first array Arr1 is: [1 2 3 4]
The second array Arr2 is: [5 6 7 8]
The third array Arr3 is: [1 2 3 4]
Arr1 and Arr2 do not have the similar elements
Arr1 and Arr3 have the similar elements
Arr2 and Arr3 do not have the similar elements

Check If Two Arrays are Equal or Not Using if Conditions

The above program has one disadvantage if we provide two arrays having the same elements but in a different order, still it treats them as unequal. We will try to overcome this disadvantage in this program.

Algorithm

Step 1 − Import the fmt package that allows us to print anything on the screen.

Step 2 − Call the main() function. This is the starting point of our program.

Step 3 − Initialize two arrays named arr1 and arr2 of integer data type.

Step 4 − Store values to these two arrays and prints them on the screen.

Step 5 − Use two for loops to iterate over the two arrays and an if condition to compare the values of the current elements of two arrays.

Step 6 − if the value turns out to be, the same increment the count variable and break the loop.

Step 7 − Repeat this process until whole arrays are checked.

Step 8 − Then by using another if condition we are comparing the length of the arrays with the count variable.

Step 9 − if the length turns out to be the same then it means that the two arrays are, same and we need to print this message on the screen. Otherwise, print that the given two arrays are not same.

Example

package main
import "fmt"
func main() {
   arr1 := [4]int{1, 2, 3, 4}
   fmt.Println("The first array Arr1 is:", arr1)
   arr2 := [4]int{2, 3, 1, 4}
   fmt.Println("The second array Arr2 is:", arr2)
   fmt.Println()
   var count int = 0
   for i := 0; i < len(arr1); i++ {
      for j := 0; j < len(arr2); j++ {
         if arr1[i] == arr2[j] {
            count++
            break
         }
      }
   }
   if (count == len(arr1)) && (count == len(arr2)) {
      fmt.Println("The above two arrays given are same")
   } else {
      fmt.Println("The above two arrays are different")
   }
}

Output

The first array Arr1 is: [1 2 3 4]
The second array Arr2 is: [2 3 1 4]

The above two arrays given are same

Check If Two Arrays are Equal or Not Using External Function

Let us now see how we can check if two arrays are equal or not using a user defined function

Example

package main
import "fmt"

// function to check if given arrays are equal or not
func equalArray(a []int, b []int) bool {
   if len(a) != len(b) {
      return false
   }
   for i, v := range a {
      if v != b[i] {
         return false
      }
   }
   return true
}
func main() {
   arr1 := []int{1, 2, 3, 4}
   fmt.Println("The first array Arr1 is:", arr1)
   arr2 := []int{1, 2, 3, 4}
   fmt.Println("The second array Arr2 is:", arr2)
   result := equalArray(arr1, arr2)
   fmt.Println()
   if result == true {
      fmt.Println("The above two arrays are equal")
   } else {
      fmt.Println("The above two arrays are not equal")
   }
}

Output

The first array Arr1 is: [1 2 3 4]
The second array Arr2 is: [1 2 3 4]

The above two arrays are equal

Conclusion

We have successfully compiled and executed a go language program to check if two arrays are equal or not along with examples.

Updated on: 28-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements