Problem · Array
Merge Two Sorted Collections Without Duplicates
Learn this problemProblem statement
You are given two integer arrays first and second, each sorted in nondecreasing order.
Return one nondecreasing integer array that contains every value appearing in either input exactly once. Duplicate occurrences within one input or across both inputs must contribute only one value to the result.
Function
mergeSortedUnique(first: int[], second: int[]) → int[]Examples
Example 1
first = [1,2,2,4]second = [2,3,4,4,5]return = [1,2,3,4,5]The value 2 appears in both inputs and 4 appears more than once, but each is included only once in the merged result.
Example 2
first = []second = [-3,-3,0,7]return = [-3,0,7]The first input is empty. Removing the repeated -3 from the second input leaves the complete result.
Example 3
first = [1,1,1]second = [1,1]return = [1]All elements have the same value, so the unique union contains one element.
Constraints
0 <= first.length, second.length <= 100000.-10^9 <= first[i], second[i] <= 10^9.- Both input arrays are sorted in nondecreasing order.