-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from kingrishabdugar/master
Efficient - Program to check if two Arrays are Equal or not
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
Efficient - Program to check if two Arrays are Equal or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
Efficient Approach: The problem can be solved efficiently using Hashing (unordered_map), | ||
|
||
Use hashing to count the frequency of each element of both arrays. Then traverse through the hashtables of the arrays and match the frequencies of the elements. | ||
|
||
Follow the below mentioned steps to solve the problem: | ||
|
||
Check if the length of both the arrays are equal or not | ||
Create an unordered map and store all elements and frequency of elements of arr1[] in the map. | ||
Traverse over arr2[] and check if count of each and every element in arr2[] matches with the count in arr1[]. This can be done by decrementing the frequency while traversing in arr2[]. | ||
|
||
Below is the implementation of the above approach: | ||
|
||
// C++ program to implement the approach | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Function to check if both arrays are equal | ||
bool checkArrays(int arr1[], int arr2[], | ||
int n, int m) | ||
{ | ||
// If lengths of arrays are not equal | ||
if (n != m) | ||
return false; | ||
|
||
// Store arr1[] elements and | ||
// their frequencies in hash map | ||
unordered_map<int, int> mp; | ||
for (int i = 0; i < n; i++) | ||
|
||
mp[arr1[i]]++; | ||
|
||
// Traverse arr2[] elements and | ||
// check if all elements of arr2[] are | ||
// present same number of times or not. | ||
for (int i = 0; i < n; i++) { | ||
|
||
// If there is an element in arr2[], | ||
// but not in arr1[] | ||
if (mp.find(arr2[i]) == mp.end()) | ||
return false; | ||
|
||
// If an element of arr2[] appears | ||
// more times than it appears in arr1[] | ||
if (mp[arr2[i]] == 0) | ||
return false; | ||
|
||
// Decrease the count of arr2 elements | ||
// in the unordered map | ||
mp[arr2[i]]--; | ||
} | ||
return true; | ||
} | ||
|
||
// Driver Code | ||
int main() | ||
{ | ||
int arr1[] = { 1, 2, 3, 4, 5 }; | ||
int arr2[] = { 4, 3, 1, 5, 2 }; | ||
int N = sizeof(arr1) / sizeof(int); | ||
int M = sizeof(arr2) / sizeof(int); | ||
|
||
// Function call | ||
if (checkArrays(arr1, arr2, N, M)) | ||
cout << "Equal"; | ||
else | ||
cout << "Not Equal"; | ||
return 0; | ||
} |