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
 
+2
omchaudhariofficialin 5 hours
C++ EASY | SIMPLE CODE
 
class Solution{
    public:

    //Function to check if two arrays are equal or not.
    bool check(vector<ll> A, vector<ll> B, int N) {
        //code here
        
        int j=0;
        
        vector<long long> arrA = A;
        vector<long long> arrB = B;
        
        sort(arrA.begin(), arrA.end());
        sort(arrB.begin(), arrB.end());
        
        for(auto &trg : arrA)
        {
            if(trg != arrB[j++])
            {
                return false;
            }
        }
        
        return true;
    }
};
0
hemasaikota5 hours ago
       A.sort()       B.sort()       if(A==B):           return True       else:           return False
0
ibrahimsaleem91236 hours ago
How is this python code ? Run time 0.41
#code here       count=0       A.sort()       B.sort()       b = zip(A,B)       for i,k in (b):           if(i==k):               count+=1       if(count==N):           return 1       else:           return 0
+1
dheeraj_giri8 hours ago
All test are correct :  Python 
 
def check(self,A,B,N):       return sorted(A) == sorted(B)
0
swatidubey9149 hours ago
Easy and Best solution in java
 
class Solution{   //Function to check if two arrays are equal or not.   public static boolean check(long A[],long B[],int N)   {       Arrays.sort(A);       Arrays.sort(B);              for(int i = 0 ; i < N ; i++){           if(A[i] != B[i]){               return false;               }       }       return true;   }}
0
amanji506715 hours ago
 bool check(vector<ll> A, vector<ll> B, int N) {       //code here       unordered_map<ll,ll> mp1,mp2;       for(int i=0;i<N;i++)       {           mp1[A[i]]++;           mp2[B[i]]++;       }       for(auto pr:mp1)       {           if(pr.second!=mp2[pr.first])           return false;       }       return true;   }
0
rajkunal1 day ago
class Solution{   public:
   //Function to check if two arrays are equal or not.   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;   }};
+1
shahabuddinbravo401 day ago
class Solution{   public:
   //Function to check if two arrays are equal or not.   bool check(vector<ll> A, vector<ll> B, int N) {
       unordered_map<ll,ll>m; // ll → long long              for(auto &value : A)       {           m[value]++;       }              for(auto &value : B){           m[value]--;       }              for(auto &value : m){           if(value.second>0)           return false;       }                     return true;   }};
-1
raviyadav80921 day ago
simple O(n ) solution . All cases passed
 
class Solution{   public:
   bool check(vector<ll> A, vector<ll> B, int N) {              sort(A.begin(), A.end());       sort(B.begin(), B.end());              int flag=1;              for(int i=0;i<N;i++)       {           if(A[i] != B[i]){               flag=1;break;}                      else               flag=0;       }              if(flag==1)       return 0;       else       return 1;                 }};
+1
kanishk2512962 days ago
    bool check(vector<ll> A, vector<ll> B, int N) {
        //code here
        unordered_set<int> m(A.begin(),A.end());
        unordered_set<int> n(B.begin(),B.end());
        int sum = 0;
        for(int i=0; i<N; i++){
            sum += (A[i]-B[i]);
            if(m.count(B[i]) == 0) return false;
            if(n.count(A[i]) == 0) return false;
        }
        return (sum == 0) ? true : false;
    }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.