Union of Two Sorted Arrays | Practice | GeeksforGeeks

Union of two arrays can be defined as the common and distinct elements in the two arrays.
Given two sorted arrays of size n and m respectively, find their union.

Example 1:
Input: 
n = 5, arr1[] = {1, 2, 3, 4, 5}  
m = 3, arr2 [] = {1, 2, 3}
Output: 1 2 3 4 5
Explanation: Distinct elements including 
both the arrays are: 1 2 3 4 5.
 
Example 2:
Input: 
n = 5, arr1[] = {2, 2, 3, 4, 5}  
m = 5, arr2[] = {1, 1, 2, 3, 4}
Output: 1 2 3 4 5
Explanation: Distinct elements including 
both the arrays are: 1 2 3 4 5.
 
Example 3:
Input:
n = 5, arr1[] = {1, 1, 1, 1, 1}
m = 5, arr2[] = {2, 2, 2, 2, 2}
Output: 1 2
Explanation: Distinct elements including 
both the arrays are: 1 2.

Your Task: 
You do not need to read input or print anything. Complete the function findUnion() that takes two arrays arr1[], arr2[], and their size n and m as input parameters and returns a list containing the union of the two arrays. 
 
Expected Time Complexity: O(n+m).
Expected Auxiliary Space: O(n+m).
 
Constraints:
1 <= n, m <= 105
1 <= arr[i], brr[i] <= 106
0
codewithshoaib193 days ago
public static ArrayList<Integer> findUnion(int arr1[], int arr2[], int n, int m)   {       // add your code here       Set<Integer> set = new HashSet<Integer>();       ArrayList<Integer> list=new ArrayList<>();       for(int i=0;i<n;i++){            set.add(arr1[i]);       }       for(int i=0;i<m;i++){            set.add(arr2[i]);       }       list.addAll(set);       Collections.sort(list);       return list;   }
0
harshscode1 week ago
 set<int> s;              for(int i=0;i<n;i++)       s.insert(a1[i]);              for(int i=0;i<m;i++)       s.insert(a2[i]);              vector<int> v;              for(auto x:s)       v.push_back(x);       return v;
0
mannukhurana103971 week ago
java
 
// all test case pass

public static ArrayList<Integer> findUnion(int arr1[], int arr2[], int n, int m) {
        // N = m+n
        // time Complexity: O(N)*O(NlogN)
        // space 2(N)
        
        Set<Integer> set = new HashSet<Integer>();

        
        int a=(m<n)?m:n;

        for(int i=0;i<a;i++){  
            set.addAll(arr1[i]);
            set.add(arr2[i]);
        }
            
        if(m<n)
            for(int i=a;i<n;i++)
                set.add(arr1[i]);
        else
            for(int i=a;i<m;i++)
                set.add(arr2[i]);
        
        
        ArrayList<Integer> lst = new ArrayList<Integer>();
        lst.addAll(set);
        Collections.sort(lst);
       
        return lst;
    }
 
// 90% test cases pass

public static ArrayList<Integer> findUnion(int arr1[], int arr2[], int n, int m) {

        // time Complexity: O(m+n)
        // space O(m+n)
        ArrayList<Integer> lst = new ArrayList<Integer>();

        int a = 0;
        int b = 0;

        while (a < n && b < m) { 
            if(arr1[a]==arr2[b]){ 
                if(!lst.contains(arr1[a]))
           
        lst.add(arr1[a]);
                a++;b++;
            }
            else if(arr1[a]<arr2[b]){ 
                if(!lst.contains(arr1[a])) lst.add(arr1[a]);
                a++;
            }
            else{
                if(!lst.contains(arr2[b])) lst.add(arr2[b]);
                b++;
            }
           
        }

        while (a < n) {
            if (!lst.contains(arr1[a]))  lst.add(arr1[a]);
            a++;
        }

        while (b < m) {
            if (!lst.contains(arr2[b])) lst.add(arr2[b]);
            b++;
        }

       
        return lst;
    }
 
 
0
nikhilpmsnick1 week ago
vector<int> findUnion(int arr1[], int arr2[], int n, int m)   {       //Your code here       //return vector with correct order of elements       vector<int> ans;              set<int> s;              for(int i=0;i<n;i++)       {           s.insert(arr1[i]);       }              for(int i=0;i<m;i++)       {           s.insert(arr2[i]);       }       for(auto it=s.begin() ; it!=s.end() ; it++)       {           ans.push_back(*it);       }       return ans;   }
0
vishutyagi71 week ago
c++ easy with stl solution
 
map<int,int> m1;       vector<int> v;       int i;       for(i=0;i<n;i++)       {           m1[arr1[i]]++;       }       for(i=0;i<m;i++)       {           m1[arr2[i]]++;       }       auto it=m1.begin();       while(it!=m1.end())       {           v.push_back(it->first);           ++it;       }       return v;
0
pradeepshillare1 week ago
...........JAVA.........
class Solution{     public static ArrayList<Integer> findUnion(int arr1[], int arr2[], int n, int m)   {              HashSet<Integer> hs = new HashSet<>();       for(int i=0;i<arr1.length;i++)       {           hs.add(arr1[i]);       }       for(int i=0;i<arr2.length;i++)       {           if(!hs.contains(arr2[i]))           {               hs.add(arr2[i]);           }       }       ArrayList<Integer> list = new ArrayList<>(hs);       Collections.sort(list);       return list;   }} 
0
nrod1231 week ago
Java Solution! super easy

    public static ArrayList<Integer> findUnion(int arr1[], int arr2[], int n, int m)
    {
        // add your code here
        TreeSet<Integer> uniqueElements = new TreeSet<>();
        if (n > m) {
            for (int i = 0; i < m; i++) {
                    uniqueElements.add(arr1[i]);
                    uniqueElements.add(arr2[i]);
            }
            for (int j = m; j < n; j++) {
                uniqueElements.add(arr1[j]);
            }
        } else {
            for (int i = 0; i < n; i++) {
                uniqueElements.add(arr1[i]);
                uniqueElements.add(arr2[i]);
            }
            for (int j = n - 1; j < m; j++) {
                uniqueElements.add(arr2[j]);
            }
        }
        
        // System.out.println(uniqueElements);
        
        return new ArrayList<>(uniqueElements);
    }
0
haulya2 weeks ago
    vector<int> findUnion(int arr1[], int arr2[], int n, int m)   {       //Your code here       //return vector with correct order of elements       int i=0;       int j = 0;       vector<int> res;              while (i < n && j < m){           if(i > 0 && arr1[i] == arr1[i-1]) { i++; continue; }                      if(j > 0 && arr2[j] == arr2[j-1]){ j++; continue; }                      if(arr1[i] == arr2[j]) {               res.push_back(arr1[i]);               i++;               j++;           }                      else if(arr1[i] < arr2[j]) {res.push_back(arr1[i]); i++;}           else if(arr1[i] > arr2[j]) {res.push_back(arr2[j]); j++;}                  }              while(i < n) {            if(i > 0 && arr1[i] == arr1[i-1]) { i++; continue; }           res.push_back(arr1[i]); i++;       }                  while (j < m) {            if(j > 0 && arr2[j] == arr2[j-1]){ j++; continue; }           res.push_back(arr2[j]); j++;       }       return res;   }
0
mayank180919992 weeks ago
 vector<int> findUnion(int arr1[], int arr2[], int n, int m)
    {
        //Your code here
        //return vector with correct order of elements
        set<int>s;
        vector<int>nums;
        for(int i=0;i<n;i++){
            s.insert(arr1[i]);
        }
        for(int i=0;i<m;i++){
            s.insert(arr2[i]);
        }
        for(auto it:s){
            nums.push_back(it);
        }
    return nums;
    }
0
shubham211019972 weeks ago
vector<int>v;       int i=0,j=0;       while(i<n1&&j<n2){           if(i>0 && arr1[i]==arr1[i-1]){               i++;               continue;           }           if(j>0 && arr2[j]==arr2[j-1]){               j++;               continue;           }           if(arr1[i]==arr2[j]){               v.push_back(arr1[i]);               i++;               j++;           }         else if(arr1[i]<arr2[j]){              v.push_back(arr1[i]);               i++;         }         else{              v.push_back(arr2[j]);              j++;         }       }       while(i<n1){           if(i>0 && arr1[i]==arr1[i-1]){               i++;               continue;           }else{               v.push_back(arr1[i]);               i++;           }                  }       while(j<n2){           if(j>0 && arr2[j]==arr2[j-1]){               j++;               continue;           }else{                v.push_back(arr2[j]);                j++;           }       }       return v;          }
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.