Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaJava Program to Check if two Arrays are Equal or not

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

  • First, we will initialize two arrays and will insert the elements in both the arrays.
  • After that, Arrays.equal() function is called to check whether the two arrays are equal or not and the result will be stored into one boolean variable namely result.
  • Finally, the result will be printed.

Example: Below is the implementation of the above approach. 

Java




// 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




// 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

  • First, we will initialize two arrays a and b and insert the elements in both the arrays. Then create a boolean variable called result to store the result after checking.
  • Then we will check the length of the arrays whether the length of the arrays are equal or not.
  • If it is equal then, loop will repeat for every element till the end of the array. If somewhere some element is not equal then we will make the result as false.
  • If the value of the result variable is false, it means the arrays are not equal.
  • If the arrays are equal then the value of the result variable will remain true.
  • And also if the length of the arrays is not equal it will return false.

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

Java




// 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




// 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)

RELATED ARTICLES

Most Popular

Recent Comments