00:00

Problem Statement

Input/Output Examples

Visual Explanation

Because both inputs are sorted, equal values reach the front together or one right after another. Comparing with the last added value is therefore enough to remove every repeat.

Approaches

The two-pointer merge uses the sorted order. The TreeSet version ignores it and sorts again.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n and m are the array lengths. The merge reads each element once; the tree set pays a logarithmic cost per insertion.

Interview Tips and Recommended Approach

  • Use the sorted order. It is what makes a linear solution possible.
  • Handle repeats within one array. Comparing with the last added value covers them.
  • Know the intersection variant. Advance both pointers only when the values are equal.
  • Watch the boxed comparison. Comparing an Integer from a list with an int unboxes it, which is safe.

Recommended Approach: Two-Pointer Merge

Why: it reads each array once, keeps the result sorted and removes duplicates without any extra data structure.