Arrays

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Use a write pointer to keep only the first copy of each value.

Step 1 / 5
Problem shape
Current lesson

Remove Duplicates From Sorted Array Lab

Arrays Beginner

Remove Duplicates From Sorted Array

Welcome to the Remove Duplicates From Sorted Array algorithm lab. Use a write pointer to keep only the first copy of each value. Use it when: Use when sorted order groups equal values together. The time complexity is O(n), and the space complexity is O(1). The pseudocode is: write = 1 for read from 1 to n - 1 if a[read] != a[read - 1] a[write] = a[read] write++ return write As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(1)

Pseudocode

write = 1
for read from 1 to n - 1
  if a[read] != a[read - 1]
    a[write] = a[read]
    write++
return write
Open deeper structure lesson
1.0x