Union of two arrays | Practice | GeeksforGeeks

Given two arrays a[] and b[] of size n and m respectively. The task is to find union between these two arrays. 
Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in the union.
Example 1:
Input:
5 3
1 2 3 4 5
1 2 3
Output: 
5
Explanation: 
1, 2, 3, 4 and 5 are the
elements which comes in the union set
of both arrays. So count is 5.

Example 2:
Input:
6 2 
85 25 1 32 54 6
85 2 
Output: 
7
Explanation: 
85, 25, 1, 32, 54, 6, and
2 are the elements which comes in the
union set of both arrays. So count is 7.
Your Task:
Complete doUnion funciton that takes a, n, b, m as parameters and returns the count of union elements of the two arrays. The printing is done by the driver code.
Constraints:
1 ≤ n, m ≤ 105
0 ≤ a[i], b[i] < 105
Expected Time Complexity : O((n+m)log(n+m))
Expected Auxilliary Space : O(n+m)
0
ranabirchakraborty21819 hours ago
Use Hashset, which doesn't allow duplicate elements.class Solution{   public static int doUnion(int a[], int n, int b[], int m)    {       int count = 0;       Set<Integer> set = new HashSet<Integer>();              for(int i=0; i<n; i++){           if(set.add(a[i])){               ++count;           }       }       for(int j=0; j<m; j++){           if(set.add(b[j])){               ++count;           }       }       return count;   }}
+1
harshscode2 days ago
 set contains unique elements......
 set<int> s;
         for(int i=0;i<n;i++)
         s.insert(a[i]);
         for(int i=0;i<m;i++)
         s.insert(b[i]);
         return s.size();
0
neetikat1662 days ago
int doUnion(int a[], int n, int b[], int m)  {       //code here       set<int> s;       for(int i=0;i<n;i++){           s.insert(a[i]);       }       for(int i=0;i<m;i++){           s.insert(b[i]);       }              return s.size();   }
0
niravramotiya0034 days ago
def doUnion(self,a,n,b,m):
       a.extend(b)
       temp=set(a)
       return(len(temp))
0
shubham211019974 days ago
public static int doUnion(int a[], int n, int b[], int m) 
   {
       HashSet<Integer>h=new HashSet<>();
       for(int i:a)
        h.add(i);
        for(int i:b)
        h.add(i);
        
        return h.size();
   }
0
vikashdas7704 days ago
   def doUnion(self,a,n,b,m):       out = a[:] + b[:]       final_list = list(set(out))       return len(final_list)
0
adityapratapsinghaps3335 days ago
int doUnion(int a[], int n, int b[], int m)  {
        map<int,int>freq;
        int i;
        
        for(i=0;i<n;i++){
            freq[a[i]]++;
        }
        
        for(i=0;i<m;i++){
            freq[b[i]]++;
        }
        
        return freq.size();
    }
0
yashshivhare5 days ago
 doUnion(a, n, b, m){   let freq={}, count=0;     for(let num of a) freq[num]?freq[num]++:freq[num]=1;     for(let num of b) freq[num]?freq[num]++:freq[num]=1;     for(let key in freq) count++;     return count;   }
0
anmolbansal25 days ago
//Java code
public static int doUnion(int a[], int n, int b[], int m)    {       Set<Integer>set=new HashSet<>();      for(int i:a){          set.add(i);      }      for(int i:b){          set.add(i);      }      return set.size();   }
0
aishaparween5461 week ago
       //we can do this by using Hashset       HashSet<Integer> set=new HashSet<>();      for(int i=0;i<n;i++) {           set.add(a[i]);                  }       for(int i=0;i<m;i++)       {           set.add(b[i]);                  }       return set.size();//which return the count of  element ofunion of two arrays          }
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.