Open In App

Java Program to Check if two Arrays are Equal or not

Given two given arrays of equal length, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain the same set of elements and in the same order. 

Note: If there are repetitions, then counts of repeated elements should be the same for two arrays to be equal.



Examples:

Input  : arr1[] = {1, 2, 5, 4, 0};
         arr2[] = {1, 2, 5, 4, 0}; 
Output : Yes

Input  : arr1[] = {1, 2, 5, 4, 0, 2};
         arr2[] = {2, 4, 5, 0}; 
Output : No
 
Input : arr1[] = {1, 7, 7};
        arr2[] = {7, 7, 1};
Output : No

Method 1: Using the pre-defined method



Example: Below is the implementation of the above approach. 




// Java Program to find the if the arrays are equal
  
import java.util.Arrays;
  
public class CheckArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 30, 25, 40 };
  
        // Initializing the second array
        int b[] = { 30, 25, 40 };
  
        // store the result
        // Arrays.equals(a, b) function is used to check
        // whether two arrays are equal or not
        boolean result = Arrays.equals(a, b);
  
        // condition to check whether the
        // result is true or false
        if (result == true) {
            // Print the result
            System.out.println("Two arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Two arrays are not equal");
        }
    }
}

Output
Two arrays are equal

Example 2:




// Java Program to find the if the arrays are equal
  
import java.util.Arrays;
  
public class CheckArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 30, 25, 40, 23 };
  
        // Initializing the second array
        int b[] = { 30, 26, 40 };
  
        // store the result
        // Arrays.equals(a, b) function is used to check
        // whether two arrays are equal or not
        boolean result = Arrays.equals(a, b);
  
        // condition to check whether the
        // result is true or false
        if (result == true) {
            // Print the result
            System.out.println("Two arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Two arrays are not equal");
        }
    }
}

Output
Two arrays are not equal

Time Complexity : O(n)
Auxiliary Space : O(1)

Method 2: Without using pre-defined function

Example 1: Below is the implementation of the above approach.




// Java Program to check if the arrays are equal
  
public class checkArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 10, 30, 12 };
        // Initializing the second array
        int b[] = { 10, 30, 12 };
  
        // store the result
        boolean result = true;
  
        // Check if length of the two arrays are equal or
        // not
        if (a.length == b.length) {
            
            // Loop to check elements of arrays one by one
            for (int i = 0; i < a.length; i = i + 1) {
                
                // To check if any element is different
                if (a[i] != b[i]) {
                    
                    // If any element is different then it
                    // will assign false into boolean
                    // variable
                    result = false;
                }
            }
        }
        else {
            
            // If the length of two arrays is
            // different then it will assign
            // false into boolean variable
            result = false;
        }
        
        // After completion to check whether
        // result is true of false
        if (result == true) {
            
            // Print the result
            System.out.println("Arrays are equal");
        }
        else {
            
            // Print the result
            System.out.println("Arrays are not equal");
        }
    }
}

Output
Arrays are equal

Example 2: 




// Java Program to check if the arrays are equal
  
public class checkArraysEqual {
    
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 10, 30, 12 };
        // Initializing the second array
        int b[] = { 45, 50, 55, 60, 65 };
  
        // stores the result
        boolean result = true;
  
        // Check if length of the two arrays are equal or
        // not
        if (a.length == b.length) {
            
            // Loop to check elements of arrays one by one
            for (int i = 0; i < a.length; i = i + 1) {
                
                // To check if any element is different
                if (a[i] != b[i]) {
                    
                    // If any element is different then it
                    // will assign false into boolean
                    // variable
                    result = false;
                }
            }
        }
        else {
            
            // If the length of two arrays is different then
            // it will assign false into boolean variable
            result = false;
        }
        
        // After completion to check whether result is true
        // of false
        if (result == true) {
            
            // Print the result
            System.out.println("Arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Arrays are not equal");
        }
    }
}

Output
Arrays are not equal

Time Complexity : O(n)
Auxiliary Space : O(1)


Article Tags :