`{"id":"22f44d6d-d7b6-494a-a549-25b4f137bc59","name":"Union Of Two Sorted Arrays","description":"1. Given two sorted arrays a[] and b[] of size n and m respectively. The task is to find union between these two arrays.\r\n2. 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 union.\r\n3. Your task is to complete Union function that takes a, b as parameters and returns an Arraylist which contains the union elements of the two arrays.The printing is done by the driver code.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1 &lt;= n, m &lt;= 10^5\r\n1 &lt;= a[i], b[i] &lt; 10^5","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid Union(vector<int> & a, vector<int>& b, vector<int>& ans){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> a(n,0);\n for(int i=0; i<n; i++){\n cin>>a[i];\n }\n int m;\n cin>>m;\n vector<int> b(m,0);\n for(int i=0; i<m; i++){\n cin>>b[i];\n }\n vector<int> ans;\n Union(a,b,ans);\n for(int i=0; i<ans.size(); i++){\n cout<<ans[i]<<\" \";\n }\n cout<<endl;\n}"},"java":{"code":"import java.util.*;\nimport java.io.*;\n\npublic class Main {\n\n /*You have to complete union function and return an Arraylist which contains union elements of the two arrays*/\n public static ArrayList<Integer> union(int[] a,int[] b) {\n //write your code here\n return null;\n }\n\n public static void main(String[]args) {\n Scanner scn = new Scanner(System.in);\n //input work\n int n = scn.nextInt();\n int[]a = new int[n];\n for(int i=0; i < n;i++) {\n a[i] = scn.nextInt();\n }\n\n int m = scn.nextInt();\n int[]b = new int[m];\n for(int i=0;i < m;i++) {\n b[i] = scn.nextInt();\n }\n\n ArrayList<Integer>ans = union(a,b);\n\n //print answer list\n for(int val : ans) {\n System.out.print(val + \" \");\n }\n System.out.println();\n }\n}"},"python":{"code":"def main():\n pass\n\nif_name_=='__main__':\n main()"}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1 2 2 3 3 4\r\n4 \r\n1 2 3 5","sampleOutput":"1 2 3 4 5","questionVideo":"https://www.youtube.com/embed/JCT04Z94Nyo","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"4fb1c62a-4b92-4f5b-95f4-5a8588a8025b","name":"Union Of Two Sorted Arrays","slug":"union-of-two-sorted-arrays","type":1}],"next":{"id":"aec536c6-06d9-4e59-9f61-4d6307e8a868","name":"Union of two sorted arrays","type":3,"slug":"union-of-two-sorted-arrays"},"prev":{"id":"aacde5dc-f504-42ba-bb24-41c184fbd800","name":"Find First And Last Postion Of Element In Sorted Array MCQ","type":0,"slug":"find-first-and-last-postion-of-element-in-sorted-array-mcq"}}}`

# Union Of Two Sorted Arrays

1. Given two sorted arrays a[] and b[] of size n and m respectively. The task is to find union between these two arrays. 2. 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 union. 3. Your task is to complete Union function that takes a, b as parameters and returns an Arraylist which contains the union elements of the two arrays.The printing is done by the driver code.

`{"id":"22f44d6d-d7b6-494a-a549-25b4f137bc59","name":"Union Of Two Sorted Arrays","description":"1. Given two sorted arrays a[] and b[] of size n and m respectively. The task is to find union between these two arrays.\r\n2. 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 union.\r\n3. Your task is to complete Union function that takes a, b as parameters and returns an Arraylist which contains the union elements of the two arrays.The printing is done by the driver code.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1 &lt;= n, m &lt;= 10^5\r\n1 &lt;= a[i], b[i] &lt; 10^5","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid Union(vector<int> & a, vector<int>& b, vector<int>& ans){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> a(n,0);\n for(int i=0; i<n; i++){\n cin>>a[i];\n }\n int m;\n cin>>m;\n vector<int> b(m,0);\n for(int i=0; i<m; i++){\n cin>>b[i];\n }\n vector<int> ans;\n Union(a,b,ans);\n for(int i=0; i<ans.size(); i++){\n cout<<ans[i]<<\" \";\n }\n cout<<endl;\n}"},"java":{"code":"import java.util.*;\nimport java.io.*;\n\npublic class Main {\n\n /*You have to complete union function and return an Arraylist which contains union elements of the two arrays*/\n public static ArrayList<Integer> union(int[] a,int[] b) {\n //write your code here\n return null;\n }\n\n public static void main(String[]args) {\n Scanner scn = new Scanner(System.in);\n //input work\n int n = scn.nextInt();\n int[]a = new int[n];\n for(int i=0; i < n;i++) {\n a[i] = scn.nextInt();\n }\n\n int m = scn.nextInt();\n int[]b = new int[m];\n for(int i=0;i < m;i++) {\n b[i] = scn.nextInt();\n }\n\n ArrayList<Integer>ans = union(a,b);\n\n //print answer list\n for(int val : ans) {\n System.out.print(val + \" \");\n }\n System.out.println();\n }\n}"},"python":{"code":"def main():\n pass\n\nif_name_=='__main__':\n main()"}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1 2 2 3 3 4\r\n4 \r\n1 2 3 5","sampleOutput":"1 2 3 4 5","questionVideo":"https://www.youtube.com/embed/JCT04Z94Nyo","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"4fb1c62a-4b92-4f5b-95f4-5a8588a8025b","name":"Union Of Two Sorted Arrays","slug":"union-of-two-sorted-arrays","type":1}],"next":{"id":"aec536c6-06d9-4e59-9f61-4d6307e8a868","name":"Union of two sorted arrays","type":3,"slug":"union-of-two-sorted-arrays"},"prev":{"id":"aacde5dc-f504-42ba-bb24-41c184fbd800","name":"Find First And Last Postion Of Element In Sorted Array MCQ","type":0,"slug":"find-first-and-last-postion-of-element-in-sorted-array-mcq"}}}` Editor

# Union Of Two Sorted Arrays

medium

1. Given two sorted arrays a[] and b[] of size n and m respectively. The task is to find union between these two arrays. 2. 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 union. 3. Your task is to complete Union function that takes a, b as parameters and returns an Arraylist which contains the union elements of the two arrays.The printing is done by the driver code.

## Constraints

1 <= n, m <= 10^5 1 <= a[i], b[i] < 10^5

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 1 2 2 3 3 4 4 1 2 3 5```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}1 2 3 4 5`

Question Video

Discussions

Show Discussion

Related Resources 