Javatpoint Logo
Javatpoint Logo

Check if two arrays are equal or not

Let us understand the question by taking some examples.

If the arrays arr1=[1,2,3,4,5] and arr2 = [5,4,3,2,1] are the two arrays, we need to check whether both arrays are equal or not.

Two arrays are considered equal if both arrays have the same elements and those elements have the same frequency in their respective arrays.

Let us take the above example:

All the elements in arr1 are also present in arr2, and every element in the arr1 occurs only once. Also, the elements in arr2 occur only once, and there are no elements present in arr1 and not present in arr2 and vice-versa.

Let us take another example:

If arr1=[1,2,3,4,4] and arr2=[1,1,2,3,4]

In the above two arrays, all the elements present in arr1 are also present in arr2, but in arr1, element 4 has occurred two times, and in arr2, it has occurred only once. Element 1 occurred twice in arr2 but only a single time in arr1, so we can conclude that both arrays are not equal.

Let us take the last example:

Arr1=[1,2,3,4] arr2=[2,3,4,5], here in this case, both arrays are not equal because element 1 is present in arr1 but not present in arr2 and element 5 is present in arr2 but not in arr1, so thereby they are not equal.

We need to deploy a code that determines whether the two input arrays are equal.

CODE:

Output:

Check if two arrays are equal or not
Check if two arrays are equal or not
Check if two arrays are equal or not

EXPLANATION:

Let us discuss the logic of the code:

We need to find out whether all the elements in arr1 are equal to all the elements of arr2 with the same frequency.

So, first, we need to sort both arrays and compare elements of both arrays from the first indexes.

i.e., after sorting both arrays, we need to check whether all the elements in both arrays are equal with their equal indexes.

The elements at a particular index should be the same because we are sorting the arrays, and for the two arrays to be equal, they also need to have the same frequency so that both arrays will have the same values at the same indexes.

So, our logic is to sort the given arrays and then compare the elements of both arrays.

First, we need to take input from the user to take the input size and the array elements.

The above lines of code will take the input from the user for n size and both array elements.

Next, we need to sort both arrays. We have a direct function called sort, which takes two array parameters.

The above two lines of code will sort the two arrays.

Next, we need to verify whether all the elements are equal at their indexes in both arrays, so to do that, we need a for loop to traverse.

And if at any index the values are not equal, we will print as both arrays are not equal; if for the entire array, all the values are equal automatically, both arrays are equal will be printed.

To print whether the arrays are equal or not, we need the above lines of code; if both are not equal, it will print that both arrays are not equal and exit from the code.

This is how we will perform the code to check whether both arrays are equal.

Let us discuss the time and space complexity:

TIME COMPLEXITY: If we ignore the time taken for the input, then the time is consumed at sorting the given arrays, and then we need to traverse the arrays to check that all index values are equal for both arrays.

So total time is O(n*log*n) for arr1 sorting + O(n*log(n)) for arr2 sorting, so for sorting we consider it as O(n*log(n))

Next, we need time to traverse, i.e., O(n).

Total taken time is O(n)+O(n*log(n)).

Finally we consider the time as O(n*log(n))

SPACE COMPLEXITY:

We haven't used any extra space here; we just used the given input arrays only, so space is constant here O(1)

Time complexity: O(n*log(n))

Space Complexity:O(1)

Let us discuss another approach to reduce the time complexity of the above code:

To reduce the time complexity of the above code, we need to use the hashmap.

The logic is. First, we need to push all the elements of array1 into the map, and then while traversing the second array, we need to reduce the count of the element in the map; if the count of the element is zero or if the element is not present in the map it indicates that there might be a different frequency occurring of the number or a missing number which is present only in one array.

If the above two conditions are not satisfied for the entire array, then we can say that both the arrays are equal.

So, let us deploy the code to find whether the two arrays are equal or not using the map.

CODE:

Output:

Check if two arrays are equal or not
Check if two arrays are equal or not
Check if two arrays are equal or not

EXPLANATION:

First, we need to take input for the size of the array and the array elements.

The above code will handle the case of taking the input from the user.

While taking the input from the user of arr1 elements, we have also pushed the elements into a map called mp.

So, while taking the input values, they are inserted into the map.

After inserting it into the map, we need to traverse the second array and check for the two conditions: one is if the element is not present in the map, and the other is if the element count is 0. If any condition is satisfied, we need to print as both arrays are unequal, and we exit from the code.

If the above two conditions are not satisfied for the entire second vector elements, we print as both arrays are equal.

We need the above code to print whether the arrays are equal or not.

And this is how we will implement the code for knowing whether the two arrays are equal.

Let us discuss the time and space complexity of the code:

TIME COMPLEXITY: If we ignore the time taken for taking the input, time is required only to traverse the second array, and in the worst case, the time taken to traverse the second array is O(n).

Time complexity: O(n).

SPACE COMPLEXITY: Here in this code, we need space for the hashmap to store the values of the first array, so the space required is O(N)

Space complexity: O(n).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA