Check if two arrays are equal or not (original) (raw)
Last Updated : 26 Dec, 2024
Given two arrays, **a and **b of equal length. The task is to determine if the given arrays are equal or not. Two arrays are considered equal if:
- Both arrays contain the same set of elements.
- The arrangements (or permutations) of elements may be different.
- If there are repeated elements, the counts of each element must be the same in both arrays.
**Examples:
**Input: a[] = [1, 2, 5, 4, 0], b[] = [2, 4, 5, 0, 1]
**Output: true**Input: a[] = [1, 2, 5, 4, 0, 2, 1], b[] = [2, 4, 5, 0, 1, 1, 2]
**Output: true**Input: a[] = [1, 7, 1], b[] = [7, 7, 1]
**Output: false
Table of Content
- [Naive Approach] Using Sorting - O(n*log n) Time and O(1) Space
- [Expected Approach] Using Hashing - O(n) Time and O(n) Space
[Naive Approach] Using Sorting - O(n*log n) Time and O(1) **Space
The basic idea is to **sort the both arrays. Compare the arrays element by element one by one if all are same then it is equal array otherwise it is not.
C++ `
// C++ program to find given two array // are equal or not #include <bits/stdc++.h> using namespace std;
bool checkEqual(vector& a, vector& b) { int n = a.size(), m = b.size();
// If lengths of array are not equal means
// array are not equal
if (n != m) return false;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for (int i = 0; i < n; i++)
if (a[i] != b[i])
return false;
// If all elements were same.
return true;}
int main() { vector a = { 3, 5, 2, 5, 2 }; vector b = { 2, 3, 5, 5, 2 };
if (checkEqual(a, b))
cout << "true";
else
cout << "false";
return 0;}
Java
import java.util.Arrays;
class GfG { static boolean checkEqual(int[] a, int[] b) {
// If lengths of array are not equal means
// array are not equal
if (a.length != b.length) return false;
Arrays.sort(a);
Arrays.sort(b);
for (int i = 0; i < a.length; i++)
if (a[i] != b[i])
return false;
// If all elements were same.
return true;
}
public static void main(String[] args) {
int[] a = { 3, 5, 2, 5, 2 };
int[] b = { 2, 3, 5, 5, 2 };
if (checkEqual(a, b))
System.out.println("true");
else
System.out.println("false");
}}
Python
def checkEqual(a, b):
# If lengths of array are not equal means
# array are not equal
if len(a) != len(b):
return False
return sorted(a) == sorted(b)if name == 'main': a = [3, 5, 2, 5, 2] b = [2, 3, 5, 5, 2]
if checkEqual(a, b):
print("true")
else:
print("false")C#
using System; using System.Linq;
class GfG { static bool CheckEqual(int[] a, int[] b) {
// If lengths of array are not equal means
// array are not equal
if (a.Length != b.Length) return false;
Array.Sort(a);
Array.Sort(b);
for (int i = 0; i < a.Length; i++)
if (a[i] != b[i])
return false;
// If all elements were same.
return true;
}
static void Main() {
int[] a = { 3, 5, 2, 5, 2 };
int[] b = { 2, 3, 5, 5, 2 };
if (CheckEqual(a, b))
Console.WriteLine("true");
else
Console.WriteLine("false");
}}
JavaScript
function checkEqual(a, b) { // If lengths of array are not equal means // array are not equal if (a.length !== b.length) return false; a.sort(); b.sort(); for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
// If all elements were same.
return true;}
//Driver Code const a = [3, 5, 2, 5, 2]; const b = [2, 3, 5, 5, 2]; console.log(checkEqual(a, b) ? 'true' : 'false');
`
**Time Complexity: O(n*log n), since sorting is used
**Auxiliary Space: O(1)
[Expected Approach] UsingHashing- O(n) Time and O(n) Space
Use of a hash map to count the occurrences of each element in one array and then verifying these counts against the second array.
C++ `
#include #include #include using namespace std;
bool checkEqual(vector& a, vector& b) { int n = a.size(), m = b.size(); if (n != m) return false;
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
mp[a[i]]++;
for (int i = 0; i < n; i++) {
if (mp.find(b[i]) == mp.end())
return false;
if (mp[b[i]] == 0)
return false;
mp[b[i]]--;
}
return true;}
int main() { vector a = { 3, 5, 2, 5, 2 }; vector b = { 2, 3, 5, 5, 2 };
if (checkEqual(a, b))
cout << "true";
else
cout << "false";
return 0;}
Java
import java.io.; import java.util.;
class GfG {
public static boolean checkEqual(int a[], int b[]) {
int n = a.length, m = b.length;
if (n != m)
return false;
Map<Integer, Integer> map
= new HashMap<Integer, Integer>();
int count = 0;
for (int i = 0; i < n; i++) {
if (map.get(a[i]) == null)
map.put(a[i], 1);
else {
count = map.get(a[i]);
count++;
map.put(a[i], count);
}
}
for (int i = 0; i < n; i++) {
if (!map.containsKey(b[i]))
return false;
if (map.get(b[i]) == 0)
return false;
count = map.get(b[i]);
--count;
map.put(b[i], count);
}
return true;
}
public static void main(String[] args) {
int a[] = { 3, 5, 2, 5, 2 };
int b[] = { 2, 3, 5, 5, 2 };
if (checkEqual(a, b))
System.out.println("true");
else
System.out.println("false");
}}
Python
def checkEqual(a, b): n, m = len(a), len(b) if n != m: return False
mp = {}
for num in a:
mp[num] = mp.get(num, 0) + 1
for num in b:
if num not in mp:
return False
if mp[num] == 0:
return False
mp[num] -= 1
return Trueif name == 'main': a = [3, 5, 2, 5, 2] b = [2, 3, 5, 5, 2]
if checkEqual(a, b):
print("true")
else:
print("false")C#
using System; using System.Collections.Generic; using System.Linq;
class GfG { static bool CheckEqual(int[] a, int[] b) { int n = a.Length, m = b.Length; if (n != m) return false;
var mp = new Dictionary<int, int>();
foreach (var num in a) {
if (mp.ContainsKey(num))
mp[num]++;
else
mp[num] = 1;
}
foreach (var num in b) {
if (!mp.ContainsKey(num) || mp[num] == 0)
return false;
mp[num]--;
}
return true;
}
static void Main() {
int[] a = { 3, 5, 2, 5, 2 };
int[] b = { 2, 3, 5, 5, 2 };
Console.WriteLine(CheckEqual(a, b) ? "true" : "false");
}}
JavaScript
function checkEqual(a, b) { const n = a.length, m = b.length; if (n !== m) return false;
const mp = {};
for (let num of a) {
mp[num] = (mp[num] || 0) + 1;
}
for (let num of b) {
if (!(num in mp) || mp[num] === 0)
return false;
mp[num]--;
}
return true;}
// Driver Code const a = [3, 5, 2, 5, 2]; const b = [2, 3, 5, 5, 2];
console.log(checkEqual(a, b) ? 'true' : 'false');
`
**Time Complexity: O(n), where n is the length of given array
**Auxiliary Space: O(n)
Check if two arrays are equal or not | DSA Problem