Advertisement
Union of Two Sorted Arrays with Distinct Elements
JavaView on GFG
Union of Two Sorted Arrays with Distinct Elements.java
Java
class Solution {
// Function to return a list containing the union of the two arrays.
public static ArrayList<Integer> findUnion(int a[], int b[]) {
HashSet<Integer> set = new HashSet<>();
ArrayList<Integer> union = new ArrayList<>();
for (int i : a)
set.add(i);
for (int j : b) {
if (!set.contains(j))
set.add(j);
}
for (int k : set)
union.add(k);
Collections.sort(union);
return union;
}
}
Advertisement
Was this solution helpful?