Check if Two Arrays Are Equal
Problem Statement
Given two arrays, check if they are equal. Two arrays are considered equal if:
- They have the same size.
- All elements of one array are present in the other array, including frequency.
- The order of elements may or may not matter depending on the variation.
👉 In this problem, we consider arrays equal regardless of order (most common interview variation).
Example 1
Input:
arr1 = [1, 2, 3, 4]
arr2 = [4, 3, 2, 1]
Output:
True
Explanation:
- Both arrays have the same elements and frequency.
- Order does not matter.
Example 2
Input:
arr1 = [1, 2, 2, 3]
arr2 = [2, 1, 3, 2]
Output:
True
Explanation:
- Elements and frequency match.
- Arrays are equal.
Example 3
Input:
arr1 = [1, 2, 3]
arr2 = [1, 2, 4]
Output:
False
Explanation:
- Element 3 in arr1 does not match element 4 in arr2.
Why This Problem Is Important
Checking array equality is a common task in:
- Data comparison
- Test validations
- Array manipulation problems
- Interview coding questions
It helps you understand:
- Frequency counting
- Sorting techniques
- Hashing for fast lookup
Approaches to Solve the Problem
- Sort and Compare
- Using Hash Maps / Frequency Counting (Efficient) ✅
Approach 1: Sort and Compare
Idea
- Sort both arrays.
- Compare elements one by one.
- If any element differs → arrays are not equal.
Time & Space Complexity
- Time Complexity: O(n log n) (due to sorting)
- Space Complexity: O(1) or O(n) depending on sorting method
Implementations
C Implementation
#include
#include
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int arr1[] = {1, 2, 2, 3};
int arr2[] = {2, 1, 3, 2};
int n = 4, m = 4;
if (n != m) {
printf("Arrays are not equal");
return 0;
}
qsort(arr1, n, sizeof(int), compare);
qsort(arr2, m, sizeof(int), compare);
for (int i = 0; i < n; i++) {
if (arr1[i] != arr2[i]) {
printf("Arrays are not equal");
return 0;
}
}
printf("Arrays are equal");
return 0;
}
C++ Implementation
#include
#include
#include
using namespace std;
int main() {
vector arr1 = {1, 2, 2, 3};
vector arr2 = {2, 1, 3, 2};
if (arr1.size() != arr2.size()) {
cout << "Arrays are not equal";
return 0;
}
sort(arr1.begin(), arr1.end());
sort(arr2.begin(), arr2.end());
cout << (arr1 == arr2 ? "Arrays are equal" : "Arrays are not equal");
return 0;
}
Java Implementation
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] arr1 = {1, 2, 2, 3};
int[] arr2 = {2, 1, 3, 2};
if (arr1.length != arr2.length) {
System.out.println("Arrays are not equal");
return;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
System.out.println(
Arrays.equals(arr1, arr2)
? "Arrays are equal"
: "Arrays are not equal"
);
}
}
Python Implementation
arr1 = [1, 2, 2, 3]
arr2 = [2, 1, 3, 2]
if sorted(arr1) == sorted(arr2):
print("Arrays are equal")
else:
print("Arrays are not equal")
C# Implementation
using System;
class Program {
static void Main() {
int[] arr1 = {1, 2, 2, 3};
int[] arr2 = {2, 1, 3, 2};
if (arr1.Length != arr2.Length) {
Console.WriteLine("Arrays are not equal");
return;
}
Array.Sort(arr1);
Array.Sort(arr2);
Console.WriteLine(
arr1.SequenceEqual(arr2)
? "Arrays are equal"
: "Arrays are not equal"
);
}
}
JavaScript Implementation
let arr1 = [1, 2, 2, 3];
let arr2 = [2, 1, 3, 2];
arr1.sort((a,b) => a - b);
arr2.sort((a,b) => a - b);
console.log(
JSON.stringify(arr1) === JSON.stringify(arr2)
? "Arrays are equal"
: "Arrays are not equal"
);
Approach 2: Using Hash Maps (Efficient)
Idea
- Create a frequency map for the first array.
- Decrease counts while traversing the second array.
- If any element is missing or count mismatches → arrays are not equal.
Time & Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
✅ Recommended for large arrays or interview scenarios.
Implementations
C Implementation
#include
#include
#define MAX 100000
int main() {
int arr1[] = {1, 2, 2, 3};
int arr2[] = {2, 1, 3, 2};
int n = 4, m = 4;
if (n != m) {
printf("Arrays are not equal");
return 0;
}
int freq[MAX] = {0};
for (int i = 0; i < n; i++)
freq[arr1[i]]++;
for (int i = 0; i < n; i++) {
if (freq[arr2[i]] == 0) {
printf("Arrays are not equal");
return 0;
}
freq[arr2[i]]--;
}
printf("Arrays are equal");
return 0;
}
C++ Implementation
#include
#include
#include
using namespace std;
int main() {
vector arr1 = {1, 2, 2, 3};
vector arr2 = {2, 1, 3, 2};
if (arr1.size() != arr2.size()) {
cout << "Arrays are not equal";
return 0;
}
unordered_map freq;
for (int num : arr1)
freq[num]++;
for (int num : arr2) {
if (freq[num] == 0) {
cout << "Arrays are not equal";
return 0;
}
freq[num]--;
}
cout << "Arrays are equal";
return 0;
}
Java Implementation
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] arr1 = {1, 2, 2, 3};
int[] arr2 = {2, 1, 3, 2};
if (arr1.length != arr2.length) {
System.out.println("Arrays are not equal");
return;
}
HashMap map = new HashMap<>();
for (int num : arr1)
map.put(num, map.getOrDefault(num, 0) + 1);
for (int num : arr2) {
if (!map.containsKey(num)) {
System.out.println("Arrays are not equal");
return;
}
map.put(num, map.get(num) - 1);
if (map.get(num) == 0)
map.remove(num);
}
System.out.println("Arrays are equal");
}
}
Python Implementation
arr1 = [1, 2, 2, 3]
arr2 = [2, 1, 3, 2]
from collections import Counter
if Counter(arr1) == Counter(arr2):
print("Arrays are equal")
else:
print("Arrays are not equal")
C# Implementation
using System;
using System.Collections.Generic;
class Program {
static void Main() {
int[] arr1 = {1, 2, 2, 3};
int[] arr2 = {2, 1, 3, 2};
if (arr1.Length != arr2.Length) {
Console.WriteLine("Arrays are not equal");
return;
}
Dictionary freq = new Dictionary();
foreach (int num in arr1)
freq[num] = freq.GetValueOrDefault(num, 0) + 1;
foreach (int num in arr2) {
if (!freq.ContainsKey(num)) {
Console.WriteLine("Arrays are not equal");
return;
}
freq[num]--;
if (freq[num] == 0)
freq.Remove(num);
}
Console.WriteLine("Arrays are equal");
}
}
JavaScript Implementation
let arr1 = [1, 2, 2, 3];
let arr2 = [2, 1, 3, 2];
if (arr1.length !== arr2.length) {
console.log("Arrays are not equal");
} else {
let map = new Map();
for (let num of arr1)
map.set(num, (map.get(num) || 0) + 1);
let equal = true;
for (let num of arr2) {
if (!map.has(num)) {
equal = false;
break;
}
map.set(num, map.get(num) - 1);
if (map.get(num) === 0)
map.delete(num);
}
console.log(equal ? "Arrays are equal" : "Arrays are not equal");
}
Dry Run (Step-by-Step)
| Step | arr1 | arr2 | Notes |
|---|---|---|---|
| Initial | [1,2,2,3] | [2,1,3,2] | Input arrays |
| Sorting | [1,2,2,3] | [1,2,2,3] | Arrays sorted |
| Compare | Element by element | All match → Equal | |
| Using Hash Map | Count frequency | Subtract while traversing arr2 | Map empty at end → Equal |
Summary
Checking if two arrays are equal is a fundamental interview question.
- Sorting is simple and works well for smaller arrays.
- Hash Map / Frequency count is efficient for large arrays and avoids O(n log n) sorting overhead.
Mastering this problem prepares you for array comparison, frequency counting, and multi-set operations.
