Java program to compare two arrays are equal or not in size

In this tutorial, you will learn how to write Java program to compare two arrays and check that they are equal in size or not.

Below are the approach which we will be follow to achieve our goal:

  • Our approach is to check two given arrays are equal in size or not is first we will find the size of both array.
  • And then compare the size. If the size are equal then it will print “size of both arrays are equal” and if the size is not equal then it will print “size of arrays are not equal”.

How our program will behave?

As we have already seen above our logic to check the size of given two arrays is equal or not.

In this program we have already two arrays. After the execution of the program it will print the output as per array size after the calculation.

Java Program to check the size of given two arrays are equal or not?

public class Main {  
    public static void main(String[] args) {
    int sum = 0;
    int arr1[]={1,2,3,4,5};
    int arr2[]={2,3,1,0,5};
    if(arr1.length == arr2.length){
        System.out.println("Array is equal");
    }else{
        System.out.println("Array is not equal");
    }
    }
}   

Output:

compare two arrays in java

Python Program to check the size of given two arrays are equal or not?

arr1=[1,2,3,4,5]
arr2=[1,3,4,5,7]
if len(arr1) == len(arr2):
    print("array is equal")
else:
    print("array is not equal") 

Output:

Leave a Comment