Learn-dsa..in 30 days!



























CC-16 : Check array equality.

Description:

Given input integer arrays arr1 and arr2, check if the arrays are equal. Arrays are said to be equal if length of both arrays is same and the element at each index in first array is equal to element at same index in the second array.

Test cases and expected outputs:

Input Parameters Expected outputs
arr1={1,2,3,4}
arr2={1,2,3,4}
The arrays are equal !
arr1={7,8}
arr2={9,8}
The arrays are *not* equal !
arr1={10,20}
arr2={30,40,50}
The arrays are *not* equal !

Pseudocode:

The java method should accept following input parameters: arr1 (int array), arr2 (int array).
Check lengths of the two arrays, if they are not same, return false.
Iterate through arr1 using a for loop using idx as loop variable:
If arr1[idx] != arr2[idx], return false.
If the above loop completes without returning false, then it means elements at all indexes are equal, so return true.

Code:

public boolean arrayCheckEquality(int[] arr1, int[] arr2) throws Exception{
	if (arr1.length != arr2.length) return false;
	for (int idx=0; idx< arr1.length; idx++) {
		if (arr1[idx] != arr2[idx]) return false;
	}	
	return true;
}

Click here to download and run code and test cases !