Remove Duplicates from Sorted Array

Easy · Arrays & Strings · about 10 minutes

Asked at Meta, Bloomberg, Google, Amazon, Microsoft.

nums is already sorted (ascending, repeats allowed). Collapse each group of repeats down to one copy, in place, keeping the values in their original order. Return k, how many distinct values there are; after the call, nums[0..k-1] must hold exactly those values (anything after index k - 1 is ignored).

Related guides