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
Elements are not necessarily distinct.
Expected Time Complexity : O((n+m)log(n+m))
Expected Auxilliary Space : O(n+m)
0
kumartanwar123in 9 hours
Easiest C++ solution:
Just make a frequency table (use unordered_map), and add both the arrays in it, and its size is the answer.
int doUnion(int a[], int n, int b[], int m)  {
        unordered_map<int, int> frequency;
        for(int i = 0;i<n;i++)
        {
            frequency[a[i]]++;
        }
        for(int i = 0;i<m;i++)
        {
            frequency[b[i]]++;
        }
        return frequency.size();
    }
0
madhusudhanreddyg72in 8 hours
class Solution{   public static int doUnion(int a[], int n, int b[], int m)    {       //Your code here       int len=m+n;       int x[]= new int [m+n];       int q=0;       for(int i=0;i<n;i++)           x[q++]=a[i];       for(int i=0;i<m;i++)           x[q++]=b[i];       Arrays.sort(x);       for(int i=1;i<m+n;i++)           if(x[i-1]==x[i])               len--;       return len;          }}
0
aakasshuitin 4 hours
Simplest Java Solution :
 
        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();
0
nishantshah1729in 1 hour
EASIEST JAVA SOLUTION:O(N)
HashSet<Integer> set=new HashSet<>();       for(int x:a)set.add(x);       for(int x:b)set.add(x);       int k=0;       return set.size();
0
jyash812611 hours ago
Here is my approach to this question
 
class Solution{   public:   //Function to return the count of number of elements in union of two arrays.   int doUnion(int a[], int n, int b[], int m)  {       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
dhanakdeepak4219 hours ago
class Solution{   public static int doUnion(int a[], int n, int b[], int m)    {       //Your code here       int c=0;       HashMap<Integer,Integer> hs=new HashMap<Integer,Integer>();       for(int i=0;i<n;i++)       {           hs.put(a[i],i);       }       for(int i=0;i<m;i++)       {           hs.put(b[i],i);       }       c=hs.size();       return c;   }}
0
dhanakdeepak4219 hours ago
class Solution{   public static int doUnion(int a[], int n, int b[], int m)    {       //Your code here       int c=0;       HashSet<Integer> hs=new HashSet<>();       for(int i=0;i<n;i++)       {           hs.add(a[i]);       }       for(int i=0;i<m;i++)       {           hs.add(b[i]);       }       c=hs.size();       return c;   }}
0
rathodumang3191 day ago
Java Solution:
class Solution{
    public static int doUnion(int a[], int n, int b[], int m) 
    {
        HashSet<Integer> set = new HashSet<>();
        for(int x : a){
            set.add(x);
        }
        for(int x : b){
            set.add(x);
        }
        return set.size();
    }
}
0
user_j52mzyl2 days ago
public static int doUnion(int a[], int n, int b[], int m) 
    {
        int[] arr = new int[n+m];
        int k=0;
        for(int i=0;i<n;i++){
            arr[k] = a[i];
            k++;
        }
        for(int j=0;j<m;j++){
            arr[k] = b[j];
            k++;
        }
        
        Arrays.sort(arr);
        int count=0;
        for(int i=0;i<k;i++){
            if(i< k-1 && arr[i]==arr[i+1]){
                count++;
                }
            }
        return k-count;
    }
0
kanishk2512963 days ago
    int doUnion(int a[], int n, int b[], int m)  {
        //code here
        unordered_set<int> res(a,a+n);
        for(int i=0; i<m; i++){
            res.insert(b[i]);
        }
        return res.size();
    }
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.