Remove Duplicates

An interactive, step-by-step visualisation of Remove Duplicates.

Category
Two Pointers
Time complexity
O(n)
Space complexity
O(1)

Pseudocode

w ← 1
for r in 1..n-1:
  if a[r] ≠ a[w-1]: a[w]=a[r]; w++

Reference implementation

w = 1
for r in range(1, len(a)):
    if a[r] != a[w-1]:
        a[w] = a[r]; w += 1
return a[:w]

Open the interactive Remove Duplicates visualisation →