Java check if two arrays are equal – Java Program to Check if Two Arrays are Equal or Not

Java check if two arrays are equal: In the previous article, we have seen Java Program to Find All Pairs of Elements in an Array Whose Product is Equal to a Specified Number

In this article we will see how to check if two arrays are equal or not.

Java Program to Check if Two Arrays are Equal or Not

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to check if two arrays are equal or not.

Method-1: Java Program to Check if Two Arrays are Equal or Not By Static Initialization of Array Elements

Approach:

  • Declare two arrays.
  • Check if both arrays have same length, if not return false.
  • Run a for loop from 0 -> arr.length and check for inequality at each index.
  • If any value is found unequal, return false.
  • If the loop is exhausted, then the arrays are equal and return true.

Program:

public class EqualArray 
{
     public static void main(String[] args)
     {
        //Two arrays are declared and initailized
        int[] arr1 = { 1, 2, 3, 4, 5, 7, 7 };
        int[] arr2 = { 1, 2, 3, 4, 5, 7, 7 };
        
        //Calling the isEqual() user defined method
        System.out.println((isEqual(arr1, arr2)));
    }

    //isEqual() method to check two arrays are equal or not
    //returns true if two arrays are equal else returns false
    public static boolean isEqual(int[] arr1, int[] arr2) 
    {
        //Checking if the length of two arrays are not equal 
        //then both arrays are not equal
        //And returning false
        if (arr1.length != arr2.length)
            return false;
        //Checking both the arrays are equal or not
        for (int i = 0; i < arr2.length; i++) 
        {
                //if any array elements mismatches 
                //then both the arrays are not equal
                if (arr1[i] != arr2[i])
                    return false;
        }
        //else returns true as the both the arrays are equal
        return true;
    }
}

Output:

true

Method-2: Java Program to Check if Two Arrays are Equal or Not By Dynamic Initialization of Array Elements

Approach:

  • Take the size input for both the arrays.
  • Declare two arrays.
  • Take input of array elements for both the arrays.
  • Check if both arrays have same length, if not return false.
  • Run a for loop from 0 -> arr.length and check for inequality at each index.
  • If any value is found unequal, return false.
  • If the loop is exhausted, then the arrays are equal and return true.
import java.util.Scanner;

public class EqualArray
{
    public static void main(String[] args)
    {
        //Object of scanner class created
        Scanner sc = new Scanner(System.in);
        //Entering size for both the arrays
        System.out.println("Enter size of first array : ");
        int m = sc.nextInt();
        System.out.println("Enter size of second array : ");
        int n = sc.nextInt();
        
        //Taking input of array elements for the first array
        System.out.println("Enter array elements for first array : ");
        int[] arr1 = new int[m];
        for (int i = 0; i < n; i++) 
        {
            arr1[i] = sc.nextInt();
        }
        
        //Taking input of array elemnts for the second array
        System.out.println("Enter array elements for second array : ");
        int[] arr2 = new int[n];
        for (int i = 0; i < n; i++) 
        {
            arr2[i] = sc.nextInt();
        }
        
        //Calling the isEqual() user defined method
        System.out.println((isEqual(arr1, arr2)));
    }

    //isEqual() method to check two arrays are equal or not 
    //returns true if two arrays are equal else returns false
    public static boolean isEqual(int[] arr1, int[] arr2) 
    {
        //Checking if the length of two arrays are not equal 
        //then both arrays are not equal 
        //And returning false
        if (arr1.length != arr2.length)
            return false;
            
        //Checking both the arrays are equal or not
        //if any array elements mismatches 
        //then both the arrays are not equal
        for (int i = 0; i < arr2.length; i++) 
        {
                if (arr1[i] != arr2[i])
                    return false;
        }
        //else returns true as the both the arrays are equal
        return true;
    }
}
Output:

Case-1
Enter size of first array : 4
Enter size of second array : 4
Enter array elements for first array : 1 2 3 4
Enter array elements for second array : 1 2 3 4
true

Case-2
Enter size of first array : 4
Enter size of second array : 4
Enter array elements for first array : 1 2 3 5
Enter array elements for second array : 1 2 3 4
true

Case-3
Enter size of first array : 5
Enter size of second array : 4
Enter array elements for first array : 1 2 3 4 5
Enter array elements for second array : 1 2 3 4
false

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: