Two arrays are considered equal if they contain the same elements with the same frequencies, regardless of their order.
- The arrays must have the same length.
- Each element must occur the same number of times in both arrays.
Examples:
Input: arr1[] = {1, 2, 3, 4, 5}, arr2[] = {5, 4, 3, 2, 1}
Output: Equal
Explanation: Both arrays contain the same elements with the same frequencies.
Input: arr1[] = {1, 2, 2, 3}, arr2[] = {1, 2, 3, 3}
Output: Not Equal
Explanation: The frequency of 2 and 3 is different in the two arrays.
Approaches to Check if Two Arrays are Equal
We can check whether two arrays are equal using the following approaches:
Sorting Approach
The simplest approach is to sort both arrays. Once sorted, equal arrays will have the same elements at the same indices.
Steps
- If the array sizes are different, return false.
- Sort both arrays.
- Compare the corresponding elements of the two arrays.
- If any pair differs, return false; otherwise, return true.
#include <iostream>
#include <algorithm>
using namespace std;
bool checkArrays(int arr1[], int arr2[], int n, int m)
{
// Arrays with different sizes cannot be equal
if (n != m)
return false;
// Sort both arrays
sort(arr1, arr1 + n);
sort(arr2, arr2 + m);
// Compare corresponding elements
for (int i = 0; i < n; i++) {
if (arr1[i] != arr2[i])
return false;
}
return true;
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {5, 4, 3, 2, 1};
int n = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
if (checkArrays(arr1, arr2, n, m))
cout << "Equal";
else
cout << "Not Equal";
return 0;
}
Output
Equal
Explanation
- Both arrays have the same size, so the program sorts them.
- After sorting, both arrays become {1, 2, 3, 4, 5}.
- The elements are compared one by one and all of them match, so the arrays are equal.
Note: The sorting approach modifies the original arrays. Use copies if the input order needs to be preserved.
Hashing Approach
Hashing can be used to compare the frequency of every element without sorting the arrays.
The idea is to store the frequency of each element from the first array in an unordered_map. While traversing the second array, decrease the corresponding frequency. If an element is missing or its frequency becomes insufficient, the arrays are not equal.
Steps
- If the array sizes are different, return false.
- Store the frequency of each element of the first array in an unordered_map.
- Traverse the second array.
- If an element is not present or its remaining frequency is zero, return false.
- Decrease the frequency of each matched element.
- If all elements are matched, return true.
#include <iostream>
#include <unordered_map>
using namespace std;
bool checkArrays(int arr1[], int arr2[], int n, int m)
{
// Arrays with different sizes cannot be equal
if (n != m)
return false;
unordered_map<int, int> freq;
// Store frequencies of elements in arr1
for (int i = 0; i < n; i++)
freq[arr1[i]]++;
// Match frequencies using arr2
for (int i = 0; i < n; i++) {
if (freq.find(arr2[i]) == freq.end()
|| freq[arr2[i]] == 0) {
return false;
}
freq[arr2[i]]--;
}
return true;
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {4, 3, 1, 5, 2};
int n = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
if (checkArrays(arr1, arr2, n, m))
cout << "Equal";
else
cout << "Not Equal";
return 0;
}
Output
Equal
Explanation
- The hash map stores the frequency of elements in arr1.
- While traversing arr2, each matched frequency is decremented.
- If any element is missing or overused, the arrays are not equal.