Skills101
← All coding problems

Remove Duplicates from Sorted Array

easyTwo Pointers+10 XPTime limit 2s

The array is sorted in non-decreasing order. Remove the duplicates so that each value appears once, keeping the order.

Input:

  • line 1: n
  • line 2: n sorted integers

Output:

  • line 1: k — the number of distinct values
  • line 2: the k distinct values in order

Constraints: 1 ≤ n ≤ 10⁵, −10⁴ ≤ value ≤ 10⁴.

Challenge: do it in place with a slow "write" pointer and a fast "read" pointer, in O(n) time and O(1) extra memory.

Sample input 1

3
1 1 2

Sample output 1

2
1 2

Sample input 2

10
0 0 1 1 1 2 2 3 3 4

Sample output 2

5
0 1 2 3 4

📖 Stuck? Learn the pattern

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.