Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

#1. Java Program To Check Equality Of Two Arrays

Posted on 02/19/2025 By admin

Write a Java program to check if two arrays are equal or not.

If you prefer watching YouTube video for this programming question then you can refer here:-

Whenever programming questions are asked in interviews especially in startups and product-based companies, the interviewer expects you to ask questions to understand problems well instead of directly jumping on writing codes. It may be one of the criteria to evaluate your candidature.

From the above statement, it is not clear if given arrays will be single or multi-dimensional and how equality is defined here. You may ask the below questions:-

  1. Will arrays be single or multidimensional?
  2. Should both arrays contain same elements in same order or same number of elements irrespective of order?

I will consider answers to the above questions below:-

  1. Arrays will be single dimensional.
  2. Both arrays should have same number of elements in the same order.

Even to be more sure you can confirm types of arrays. Just in case of negative scenarios one array may be an int while another String.

The next step is to think about logic. Once you get the logic you can write code.

There are already readymade methods named equals() and deepEquals() provided by class “Arrays” but in interviews, we are not allowed to use these methods.

int[] arr1 = {10,20,30,40};
int[] arr2 = {10,20,30,40};

  • Let’s take above two int arrays as example. To check equality you might be loooking at elements of both arrays but don’t you think first we should check the equality of lengths of both arrays first as that will be basic entry criteria before we go for further step.
  • If lengths of arrays are not same then why even we should go and iterate through both arrays to check equality of elements. Lengths of both arrays are not equal means arrays are not equals.
  • If lengths are matched then we should iterate arrays to match if each indexe has same elements in both arrays.
  • If at any index, elements are not equal in both arrays then we should terminate the loop then and there as there is no meaning of checking for remaining indexes as we know that both arrays are not equal.

The below flowchart can summerize above logic:-

Now you can write Java code easily implementing above logics.

package arraysprograms;

public class CheckForArrayEquality {

        public static void main(String[] args) {

                int[] arr1 = { 10, 20, 30, 40 };
                int[] arr2 = { 10, 20, 30, 40 };

                // Let's assume both arrays are equal.
                boolean isEqual = true;

                // We need to check length of arrays
                if (arr1.length == arr2.length) {
                        // As lengths of arrays are same, we should iterate arrays now
                        for (int i = 0; i < arr1.length; i++) {
                                // If elements are not equals at any index, set the value false and creak loop
                                if (arr1[i] != arr2[i]) {
                                        isEqual = false;
                                        break;
                                }
                        }
                }
                // Lengths are not same i.e arrays are not equal.
                else
                        isEqual = false;

                if (isEqual)
                        System.out.println("Arrays are equal");
                else
                        System.out.println("Arrays are not equal");

        }

}
For inputs:-
int[] arr1 = { 10, 20, 30, 40 };
int[] arr2 = { 10, 20, 30, 40 };
Arrays are equal

For inputs:-
int[] arr1 = { 10, 20, 30, 40 };
int[] arr2 = { 10, 200, 30, 40 };
Arrays are not equal

For inputs:-
int[] arr1 = { 10, 20, 30, 40 };
int[] arr2 = { 10, 200, 30, 40, 50};
Arrays are not equal

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Uncategorized

Post navigation

Previous Post: Part 9: Usages Of Javascripts In Selenium: How To Scroll Page By Page In Selenium WebDriver Using Javascript
Next Post: Postman Tutorial Part 44 – Data Driven Testing in Postman Using CSV File

Related Posts

TestNG Tutorials 12: How To Run Inner TestNG Class From TestNG.xml ? Uncategorized
May 10, 2017 – Make Selenium Easy Uncategorized
July 2019 – Make Selenium Easy Uncategorized
July 30, 2019 – Make Selenium Easy Uncategorized
DevTools – Make Selenium Easy Uncategorized
InitialScroll – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark