Intersection of two arrays | Practice | GeeksforGeeks

Given two arrays a[] and b[] respectively of size n and m, the task is to print the count of elements in the intersection (or common elements) of the two arrays.
For this question, the intersection of two arrays can be defined as the set containing distinct common elements between the two arrays. 
Example 1:
Input:
n = 5, m = 3
a[] = {89, 24, 75, 11, 23}
b[] = {89, 2, 4}

Output: 1

Explanation: 
89 is the only element 
in the intersection of two arrays.
Example 2:
Input:
n = 6, m = 5
a[] = {1, 2, 3, 4, 5, 6}
b[] = {3, 4, 5, 6, 7} 

Output: 4

Explanation: 
3 4 5 and 6 are the elements 
in the intersection of two arrays.

Your Task:
You don't need to read input or print anything. Your task is to complete the function NumberofElementsInIntersection() which takes two integers n and m and their respective arrays a[] and b[]  as input. The function should return the count of the number of elements in the intersection.
 
Expected Time Complexity: O(n + m).
Expected Auxiliary Space: O(min(n,m)).
Constraints:
1 ≤ n, m ≤ 105
1 ≤ a[i], b[i] ≤ 105
0
gfgcourse001 day ago
int NumberofElementsInIntersection(int a[], int b[], int n, int m) {       unordered_set<int> h(a,a+n);       unordered_set<int> h1(b,b+m);   int count=0;   for(auto x:h1)   {       if(h.find(x)!=h.end())           count++;   }   return count;   }
-1
yashvardhanyadav82 days ago
int deleteDuplicates(int arr[],int n) {       int index=0;       sort(arr,arr+n);          for (int i=0; i<n-1; i++) {           if (arr[i] != arr[i+1]) {               arr[index++] = arr[i];           }       }          arr[index++] = arr[n-1];       return index;   }
   int NumberofElementsInIntersection(int a[], int b[], int n, int m) {       int mergedArray[n+m] , size=0 , count=0;          int size1 = deleteDuplicates(a,n);        int size2 = deleteDuplicates(b,m);          for (int i=0; i<size1; i++) {           mergedArray[size++] = a[i];       }          for (int i=0; i<size2; i++) {           mergedArray[size++] = b[i];       }          sort(mergedArray , mergedArray+size);          for (int i=0; i<size-1; i++) {           if (mergedArray[i] == mergedArray[i+1]) {               count++;           }       }          return count;   }
+1
aakasshuit3 days ago
Easy Java Solution:
 
        int ans=0;
        HashSet<Integer> set = new HashSet<>();
        
        for(int i=0;i<n;i++){
            set.add(a[i]);
        }
        for(int i=0;i<m;i++){
            if(set.contains(b[i])){
                ans++;
                set.remove(b[i]);
            }
        }
        return ans;
0
vijeayaetv04 days ago
Python without using set:
 
       di={}       c=0       for i in a:           di[i]=1       for j in b:           if j in di:               if di[j]==1:                   c+=1                   di[j]+=1        return c
 
0
vijeayaetv04 days ago
Python:
 
       a=set(a)       b=set(b)       a.intersection_update(b)       c=len(list(a))       return c
0
dhairyashah1105015 days ago
class Solution { public:   int NumberofElementsInIntersection(int a[], int b[], int n, int m) {   set<int> s;   set<int> s1, s2;   for(int i=0; i<n; i++)       s1.insert(a[i]);   for(int i=0; i<m; i++)       s2.insert(b[i]);   int p = s1.size();   int q = s2.size();   set<int, greater <int>>::iterator itr;   for(itr=s1.begin(); itr!=s1.end(); itr++)       s.insert(*itr);   for(itr=s2.begin(); itr!=s2.end(); itr++)       s.insert(*itr);   int ans = p + q - s.size();   return ans;   }};
0
surajpatwa15065 days ago
   HashSet<Integer> set=new HashSet<>();for(int i:a){   set.add(i);}int c=0;for(int j:b){   if(set.contains(j)){       c++;       set.remove(j);   }   }return c;   
 
0
junaidansari219755 days ago
int c = 0;       unordered_map<int,int>mp1;       unordered_map<int,int>mp2;       for(int i=0;i<n;i++)           mp1[a[i]]++;       for(int i=0;i<m;i++)           mp2[b[i]]++;                  for(int i=0;i<n;i++)       {           if(mp2.find(a[i]) != mp2.end())           {               c++;               mp2.erase(a[i]);           }       }                     return c;
0
x7h0tps0opo0jtmmi9cv0xdwytn9d6lxflna9dq86 days ago
//Using Unordered Set
int res=0;unordered_set<int> p(a,a+n) ;for(int i=0;i<m;i++){   if(p.find(b[i])!=p.end()){   res++;   p.erase(b[i]);   }}return res;
+1
hoodninja6 days ago
            unordered_map<int,int> u;
            int count = 0;
            if(n<m)
            {
                for(int i=0;i<n;i++)
                {
                    u[a[i]]++;
                }
                for(int i=0;i<m;i++)
                {
                    if(u.find(b[i])!=u.end())
                    {
                        count++;
                        u.erase(b[i]);
                    }
                }
                
            }
            else
            {
                for(int i=0;i<m;i++)
                {
                    u[b[i]]++;
                }
                for(int i=0;i<n;i++)
                {
                    if(u.find(a[i])!=u.end())
                    {
                        count++;
                        u.erase(a[i]);
                    }
                }        
            }
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.