two-pointers · Problem 1 of 4
Remove Duplicates from Sorted Array
medium
Meta
Microsoft
Given a sorted array, remove duplicates in place so each value appears once. Return the array truncated to the unique values.
removeDuplicates([1, 1, 2, 3, 3]) -> [1, 2, 3]Your solution
Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.
Running is free — Submit is what records it. Or press ⌘↩
Tests
4 cases, 1 hidden| call | type | expected | result |
|---|---|---|---|
| removeDuplicates([1,1,2,3,3]) | with duplicates | [1,2,3] | — |
| removeDuplicates([2,2,2]) | all same | [2] | — |
| removeDuplicates([1,2,3]) | already unique | [1,2,3] | — |
| withheld | hidden | withheld | — |
Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.
Complexity
- target time
- O(n)
- target space
- O(1) extra
Same-direction pointers at different speeds. Building a new array would be O(n) space; the in-place version is the point of the exercise.
Hints
Stuck? Hints open one at a time, each giving a little more away.
3 hints left