Java Program to Check if two Arrays are Equal or not


Introduction

In Java, we can check if two arrays are equal or not by comparing their elements. If the elements in both arrays are the same and appear in the same order, then the two arrays are considered equal.

One way to check the equality of two arrays is to use the Arrays.equals method provided by the java.util package. This method takes two arrays as arguments and returns a boolean value indicating whether they are equal or not. The method compares the elements of the arrays in the same order, so if the order of the elements is not important, then we need to sort the arrays first before comparing them using this method.

Another way to check the equality of two arrays is to write our own method. We can create a method that iterates over the elements of both arrays and compares their values. If any pair of corresponding elements is not equal, then the method returns false. If all corresponding elements of both arrays are equal, then the method returns true. This approach is simple and allows us to customize the comparison logic according to our requirements. However, it requires more code and may not be as efficient as the built-in method.

Methods

Method 1: Using the Predefined Method

Arrays.equals method, which is a built-in method provided by the java.util package. This approach is simple and efficient, as the Arrays.equals method takes care of the comparison logic for us. However, it's important to note that this method compares the elements of the arrays in the same order, so if the order of the elements is not important, then we need to sort the arrays first before comparing them using this method.

Approach

  • First, we declare and initialize two integer arrays arr1 and arr2 with the same elements.

  • Next, we call the Arrays.equals method with arr1 and arr2 as arguments, and store the return value in the boolean variable isEqual.

  • The Arrays.equals method compares the elements of the two arrays in the same order and returns true if they are equal, and false otherwise.

  • Finally, we print a message indicating whether the two arrays are equal or not, based on the value of isEqual.

Example

This Java program checks the equality of two arrays using the Arrays.equals method provided by the java.util package.

import java.util.Arrays;

public class ArrayEquality {

   public static void main(String[] args) {
      int[] arr1 = {1, 2, 3, 4};
      int[] arr2 = {1, 2, 3, 4};
      boolean isEqual = Arrays.equals(arr1, arr2);
      if (isEqual) {
         System.out.println("The two arrays are equal.");
      } else {
         System.out.println("The two arrays are not equal.");
      }
   }
}

Explanation

In this approach, we are using the Arrays.equals method to compare the two arrays. This method takes two arrays as input and returns a boolean value indicating whether they are equal or not. The method checks the length of the arrays first, and then iterates over the elements of the arrays and checks if they are equal.

In the main method, we have defined two integer arrays arr1 and arr2 and passed them as arguments to the Arrays.equals method. We have then printed a message indicating whether the arrays are equal or not based on the return value of the method.

Output

This approach is simpler and more concise than the previous one, but it may not be as efficient for very large arrays, since the Arrays.equals method creates a temporary array to store the results of the comparison.

The two arrays are equal.

Method 2: Without using pre-defined Function

This method can be extended to work with arrays of other types by changing the method signature and the comparison operator in the loop that compares the elements of the arrays.

Approach

  • Define a method checkEquality that takes two arrays as input and returns a boolean value indicating whether the arrays are equal or not.

  • First, check if either of the arrays is null or if they have different lengths. If either of these conditions is true, return false.

  • If both arrays have the same length, iterate over the elements of both arrays and compare the corresponding elements. If any pair of corresponding elements is not equal, return false.

  • If the method has not yet returned false, it means that all corresponding elements of both arrays are equal. Therefore, return true.

  • In the main method, define two arrays and pass them as arguments to the checkEquality method. Print a message indicating whether the arrays are equal or not based on the return value of the method.

Example

public class ArrayEquality {

   public static void main(String[] args) {
      int[] arr1 = {1, 2, 3, 4};
      int[] arr2 = {1, 2, 3, 4};
      boolean isEqual = checkEquality(arr1, arr2);
      if (isEqual) {
         System.out.println("The two arrays are equal.");
      } else {
         System.out.println("The two arrays are not equal.");
      }
   }

   public static boolean checkEquality(int[] arr1, int[] arr2) {
      if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
         return false;
      }
      for (int i = 0; i < arr1.length; i++) {
         if (arr1[i] != arr2[i]) {
            return false;
         }
      }
      return true;
   }
}

Explanation

In this program, we have defined a checkEquality method that takes two integer arrays as parameters and returns a boolean value indicating whether they are equal or not. The method first checks if either array is null or if they have different lengths, in which case it immediately returns false. Otherwise, it iterates over the elements of the arrays and checks if they are equal. If it finds any mismatched elements, it returns false, otherwise it returns true.

Output

The two arrays are equal.

In the main method, we have defined two integer arrays arr1 and arr2 and passed them as parameters to the checkEquality method. We have then printed a message indicating whether the arrays are equal or not based on the return value of the method. In this case, since both arrays have the same elements in the same order, the program will output "The two arrays are equal." If the arrays had different elements or were in a different order, the program would output "The two arrays are not equal."

Conclusion

  • In Java, we can check if two arrays are equal or not by comparing their elements. There are different approaches to achieve this, including using the built-in Arrays.equals method, writing our own method to compare the elements, or using other techniques such as sorting or hashing. If the order of the elements is not important, then we can sort the arrays first before comparing them using the Arrays.equals method, or use other techniques such as hashing or converting the arrays to sets.

  • The Arrays.equals method provided by the java.util package is a simple and efficient way to check the equality of two arrays. It compares the elements of the two arrays in the same order and returns true if they are equal, and false otherwise. This method is suitable for most cases where the order of the elements is important.

Updated on: 10-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements