Check if two arrays are equal or not | Practice | GeeksforGeeks

Given two arrays A and B of equal size N, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though.
Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal.
Example 1:
Input:
N = 5
A[] = {1,2,5,4,0}
B[] = {2,4,5,0,1}
Output: 1
Explanation: Both the array can be 
rearranged to {0,1,2,4,5}

Example 2:
Input:
N = 3
A[] = {1,2,5}
B[] = {2,4,15}
Output: 0
Explanation: A[] and B[] have only 
one common value.
Your Task:
Complete check() function which takes both the given array and their size as function arguments and returns true if the arrays are equal else returns false.The 0 and 1 printing is done by the driver code.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(N)
Constraints:
1<=N<=107
1<=A[],B[]<=1018
 
0
adamyasharma4933 days ago
bool check(vector<ll> A, vector<ll> B, int N) {       //code here       unordered_map<ll,int> m ;               for(int i=0 ; i< N ; i++)       {           m[A[i]]++ ;        }              for(int i=0 ; i< N ;i++)       {           if(m.find(B[i]) != m.end())           { m[B[i]]-- ; }            else           {return false ; }        }              for(auto x : m)       {           if(x.second != 0)return false ;        }       return true ;    }
0
samuelgroth4 days ago
Java (1.8)
 
Arrays.sort(A);       Arrays.sort(B);              for (int i = 0; i < N; i++)            if ( A[i] != B[i] )               return false;       return true;
+1
nikhilnaroliya69534 days ago
 bool check(vector<ll> A, vector<ll> B, int N) {       //code here      int  count=0;       int result=0;        sort(A.begin(),A.end());        sort(B.begin(),B.end());        for(int i=0;i<N;i++)         {             if(A[i]==B[i])               count++;         }         if(count==N)           result=1;           return result;   }
0
ahmadaeccse
This comment was deleted.
0
faycalsawadogo4 days ago
#User function Template for python3
from collections import Counter

class Solution:
    #Function to check if two arrays are equal or not.
    def check(self,A,B,N):
        return Counter(A) == Counter(B)
0
amitrrrvaa00035 days ago
Python:
               A.sort()
               B.sort()
               if A==B:
                     return True
               else:
                     return False
+1
priyansh708905 days ago
    bool check(vector<ll> A, vector<ll> B, int N) {       //code here       sort(A.begin(),A.end());       sort(B.begin(),B.end());              return A==B;   }
+2
harshscode6 days ago
 unordered_map<long long,long long> m;       for(auto i:a)       m[i]++;       for(auto i:b)       m[i]--;       for(auto i:a)       {           if(m[i]!=0)           return 0;       }       return 1;
0
shubham211019976 days ago
simplest solution
 public static boolean check(long A[],long B[],int N)
   {
       HashMap<Long,Integer>m=new HashMap<>();
       for(long i:A){
           m.put(i,m.getOrDefault(i,0)+1);
       }
      
       for(long i:B){
           if(m.containsKey(i)==false) return false;
           else{
               m.put(i,m.get(i)-1);
           }
       }
     
       for(long i:A){
         if(m.get(i)!=0)return false;
       }
     
       return true;
       
   }
0
subhaduleygba6 days ago
//c++ code 
bool check(vector<ll> A, vector<ll> B, int N) {       //code here     sort(A.begin(),A.end());     sort(B.begin(),B.end());     for(int i=0;i<N;i++){         if(A[i]!=B[i]){             return false;         }     }     return true;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.