Check if two arrays are equal or not
Last Updated :
03 Oct, 2023
Given two arrays, arr1 and arr2 of equal length N, the task is to find if the given arrays are equal or not.
Two arrays are said to be equal if:
- both of them contain the same set of elements,
- arrangements (or permutations) of elements might/might not be same.
- If there are repetitions, then counts of repeated elements must also be the same for two arrays to be equal.
Examples:
Input: arr1[] = {1, 2, 5, 4, 0}, arr2[] = {2, 4, 5, 0, 1}
Output: Yes
Input: arr1[] = {1, 2, 5, 4, 0, 2, 1}, arr2[] = {2, 4, 5, 0, 1, 1, 2}
Output: Yes
Input: arr1[] = {1, 7, 1}, arr2[] = {7, 7, 1}
Output: No
Check if two arrays are equal or not using Sorting
Follow the steps below to solve the problem using this approach:
- Sort both the arrays
- Then linearly compare elements of both the arrays
- If all are equal then return true, else return false
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool areEqual( int arr1[], int arr2[], int N, int M)
{
if (N != M)
return false ;
sort(arr1, arr1 + N);
sort(arr2, arr2 + M);
for ( int i = 0; i < N; i++)
if (arr1[i] != arr2[i])
return false ;
return true ;
}
int main()
{
int arr1[] = { 3, 5, 2, 5, 2 };
int arr2[] = { 2, 3, 5, 5, 2 };
int N = sizeof (arr1) / sizeof ( int );
int M = sizeof (arr2) / sizeof ( int );
if (areEqual(arr1, arr2, N, M))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static boolean areEqual( int arr1[], int arr2[])
{
int N = arr1.length;
int M = arr2.length;
if (N != M)
return false ;
Arrays.sort(arr1);
Arrays.sort(arr2);
for ( int i = 0 ; i < N; i++)
if (arr1[i] != arr2[i])
return false ;
return true ;
}
public static void main(String[] args)
{
int arr1[] = { 3 , 5 , 2 , 5 , 2 };
int arr2[] = { 2 , 3 , 5 , 5 , 2 };
if (areEqual(arr1, arr2))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def areEqual(arr1, arr2, N, M):
if (N ! = M):
return False
arr1.sort()
arr2.sort()
for i in range ( 0 , N):
if (arr1[i] ! = arr2[i]):
return False
return True
if __name__ = = "__main__" :
arr1 = [ 3 , 5 , 2 , 5 , 2 ]
arr2 = [ 2 , 3 , 5 , 5 , 2 ]
n = len (arr1)
m = len (arr2)
if (areEqual(arr1, arr2, n, m)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
public static bool areEqual( int [] arr1, int [] arr2)
{
int N = arr1.Length;
int M = arr2.Length;
if (N != M)
return false ;
Array.Sort(arr1);
Array.Sort(arr2);
for ( int i = 0; i < N; i++)
if (arr1[i] != arr2[i])
return false ;
return true ;
}
public static void Main()
{
int [] arr1 = { 3, 5, 2, 5, 2 };
int [] arr2 = { 2, 3, 5, 5, 2 };
if (areEqual(arr1, arr2))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function areEqual(arr1, arr2)
{
let N = arr1.length;
let M = arr2.length;
if (N != M)
return false ;
arr1.sort();
arr2.sort();
for (let i = 0; i < N; i++)
if (arr1[i] != arr2[i])
return false ;
return true ;
}
let arr1 = [3, 5, 2, 5, 2];
let arr2 = [2, 3, 5, 5, 2];
if (areEqual(arr1, arr2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function areEqual( $arr1 , $arr2 , $N , $M )
{
if ( $N != $M )
return false;
sort( $arr1 );
sort( $arr2 );
for ( $i = 0; $i < $N ; $i ++)
if ( $arr1 [ $i ] != $arr2 [ $i ])
return false;
return true;
}
$arr1 = array (3, 5, 2, 5, 2);
$arr2 = array (2, 3, 5, 5, 2);
$N = count ( $arr1 );
$M = count ( $arr2 );
if (areEqual( $arr1 , $arr2 , $N , $M ))
echo "Yes" ;
else
echo "No" ;
?>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Check if two arrays are equal or not using Hashing:
Store count of all elements of arr1[] in a hash table. Then traverse arr2[] and check if the count of every element in arr2[] matches with the count of elements of arr1[].
Follow the steps mentioned below to implement the approach:
- First check if length of arr1 is not equal to the length of arr2 then return false
- Then traverse over first array and store the count of every element in the hash map
- Then traverse over second array and decrease the count of its elements in the hash map. If that element is not present or the count of that element is
zero in the hash map, then return false, else decrease the count of that element
- Return true at the end, as both the arrays are equal by now
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool areEqual( int arr1[], int arr2[], int N, int M)
{
if (N != M)
return false ;
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++)
mp[arr1[i]]++;
for ( int i = 0; i < N; i++) {
if (mp.find(arr2[i]) == mp.end())
return false ;
if (mp[arr2[i]] == 0)
return false ;
mp[arr2[i]]--;
}
return true ;
}
int main()
{
int arr1[] = { 3, 5, 2, 5, 2 };
int arr2[] = { 2, 3, 5, 5, 2 };
int N = sizeof (arr1) / sizeof ( int );
int M = sizeof (arr2) / sizeof ( int );
if (areEqual(arr1, arr2, N, M))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static boolean areEqual( int arr1[], int arr2[])
{
int N = arr1.length;
int M = arr2.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(arr1[i]) == null )
map.put(arr1[i], 1 );
else {
count = map.get(arr1[i]);
count++;
map.put(arr1[i], count);
}
}
for ( int i = 0 ; i < N; i++) {
if (!map.containsKey(arr2[i]))
return false ;
if (map.get(arr2[i]) == 0 )
return false ;
count = map.get(arr2[i]);
--count;
map.put(arr2[i], count);
}
return true ;
}
public static void main(String[] args)
{
int arr1[] = { 3 , 5 , 2 , 5 , 2 };
int arr2[] = { 2 , 3 , 5 , 5 , 2 };
if (areEqual(arr1, arr2))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def is_arr_equal(arr1, arr2):
if len (arr1) ! = len (arr2):
return False
count = {}
for i in arr1:
if i in count:
count[i] + = 1
else :
count[i] = 1
for i in arr2:
if i not in count or count[i] = = 0 :
return False
else :
count[i] - = 1
return True
if __name__ = = "__main__" :
arr1 = [ 3 , 5 , 2 , 5 , 2 ]
arr2 = [ 2 , 3 , 5 , 5 , 2 ]
if is_arr_equal(arr1, arr2):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static bool areEqual( int [] arr1, int [] arr2)
{
int N = arr1.Length;
int M = arr2.Length;
if (N != M)
return false ;
Dictionary< int , int > map
= new Dictionary< int , int >();
int count = 0;
for ( int i = 0; i < N; i++) {
if (!map.ContainsKey(arr1[i]))
map.Add(arr1[i], 1);
else {
count = map[arr1[i]];
count++;
map.Remove(arr1[i]);
map.Add(arr1[i], count);
}
}
for ( int i = 0; i < N; i++) {
if (!map.ContainsKey(arr2[i]))
return false ;
if (map[arr2[i]] == 0)
return false ;
count = map[arr2[i]];
--count;
if (!map.ContainsKey(arr2[i]))
map.Add(arr2[i], count);
}
return true ;
}
public static void Main(String[] args)
{
int [] arr1 = { 3, 5, 2, 5, 2 };
int [] arr2 = { 2, 3, 5, 5, 2 };
if (areEqual(arr1, arr2))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function areEqual(arr1, arr2)
{
let N = arr1.length;
let M = arr2.length;
if (N != M)
return false ;
let map
= new Map();
let count = 0;
for (let i = 0; i < N; i++) {
if (map.get(arr1[i]) == null )
map.set(arr1[i], 1);
else {
count = map.get(arr1[i]);
count++;
map.set(arr1[i], count);
}
}
for (let i = 0; i < N; i++) {
if (!map.has(arr2[i]))
return false ;
if (map.get(arr2[i]) == 0)
return false ;
count = map.get(arr2[i]);
--count;
map.set(arr2[i], count);
}
return true ;
}
let arr1 = [3, 5, 2, 5, 2];
let arr2 = [2, 3, 5, 5, 2];
if (areEqual(arr1, arr2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...