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
+1
kashyapjhon1 day ago
C++ Solution Time=(0.63/2.25) EASY:
int NumberofElementsInIntersection(int a[], int b[], int n, int m) {       // Your code goes here       unordered_set<int> u;       for(int i=0;i<n;i++){           u.insert(a[i]);       }       int c=0;       for(int i=0;i<m;i++){           auto it= u.find(b[i]);           if(it!=u.end()){               c++;               u.erase(b[i]);           }       }       return c;   }
+1
shubham211019976 days ago
public static int NumberofElementsInIntersection(int a[], int b[], int n, int m) {
       
       HashSet<Integer>h=new HashSet<>();
       for(int i:a){
           h.add(i);
       }
       int res=0;
       for(int i:b){
           if(h.contains(i)){
               res++;
               h.remove(i);
           }
       }
       return res;
   }
0
raghavendra7776 days ago
Python 0.38S
def NumberofElementsInIntersection(self,a, b, n, m):       #return: expected length of the intersection array.       a=set(a)       b=set(b)       z=a.intersection(b)       return len(z)
0
mehtay0376 days ago
Python Solution:
class Solution:   def NumberofElementsInIntersection(self,a, b, n, m):
         return len(set(a).intersection(set(b)))
 
 
0
harshscode1 week ago
    //   set contains unique elements..   // map conatains duplicate elements........     set<int> m;      for(int i=0;i<n;i++)   m.insert(a[i]);          int cnt=0;      for(int i=0;i<n1;i++)   {       if(m.find(b[i])!=m.end())      { m.erase(b[i]);       cnt++;         }      }
0
ahadfardeen1 week ago
DON'T IGNORE TO REMOVE THE ALREADY COUNT ELEMENT FROM THE ARRAY OTHERWISE IT WOULD BE COUNT MULTIPLE TIMES
 
class Solution {
   // Function to return the count of the number of elements in
   // the intersection of two arrays.
   public static int NumberofElementsInIntersection(int a[], int b[], int n, int m) {
       // Your code here
       HashSet<Integer>hset= new HashSet<>();
       for(int i=0;i<n;i++){
           hset.add(a[i]);
       }
       int count=0;
       for(int j=0;j<m;j++){
           if(hset.contains(b[j])){
           count++;
           hset.remove(b[j]);
           }
       }
       return count;
   }
};
0
hr15171517hr1 week ago
HashSet<Integer> hs1 = new HashSet<>(), hs2 = new HashSet<>();
        int res = 0;
        for (int i = 0; i < n; i++) {
            hs1.add(a[i]);
        }
        for (int i = 0; i < m; i++) {
            if (hs1.contains(b[i]) && !hs2.contains(b[i])) {
                hs2.add(b[i]);
                res++;
            }
        }
        return res;
0
mayank180919992 weeks ago
 int NumberofElementsInIntersection(int a[], int b[], int n, int m) {
        
        // Your code goes here
       unordered_map<int,int>mp;
        set<int>s;
        for(int i=0;i<n;i++){
            mp[a[i]]++;
        }
        for(int i=0;i<m;i++){
            if(mp[b[i]]){
                s.insert(b[i]);
                mp[b[i]]--;
            }
        }
      return s.size();  
    }
0
abera25832 weeks ago
what wrong in my code?
unordered_map<int,int>map;       for(int i=0;i<n;i++){           map[a[i]]++;       }       for(int i=0;i<m;i++){           map[b[i]]++;       }       int count=0;       for(auto it:map){           if(it.second!=1){               count++;           }       }       return count;   }
0
shrustis1763 weeks ago
C++ solution using MAPS
 //why erase? bcoz intersection has to be unique, so we need to count only once and to prevent it from counting again we erase.
 
int NumberofElementsInIntersection(int a[], int b[], int n, int m) {       // Your code goes here       unordered_map<int,int>mp;           int cnt=0;              for(int i=0;i<n; i++)           mp[a[i]]++;              for(int i=0; i<m; i++)          if(mp.count(b[i]))          {             mp.erase(b[i]);                            cnt++;          }   return cnt;   }
 
 
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.