Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Remove Duplicates from Sorted

Arrays & Two-Pointersmedium

Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods

Implement `removeDuplicatesSorted(arr)` returning the unique values from a sorted array, in order.

Sample tests

Input: removeDuplicatesSorted([1,1,2,3,3])
Output: [1,2,3]
Input: removeDuplicatesSorted([1])
Output: [1]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Using a Set works but ignores the sorted property; comparing neighbors is O(1) space beyond output.

Learning resources

  • Wikipedia: Data structure
Approach & explanation (try first)

Because duplicates are adjacent in sorted input, comparing each value to the last kept one removes them in one pass.

Loading...
⌘/Ctrl + Enter

Run your code to see results.