Skip to content

Find the Union ​

Find the Union — GeeksforGeeks

Given two sorted arrays, return their union — every distinct element from both arrays, in sorted order.

Approach ​

Two pointers on both arrays and move. The trick is that aside from comparing a[i] == b[i] we should also compare the last element in the union array with a[i] and b[i]. Also, copy the remaining items in the array.

Remarks ​