beginner · two pointers · read/write pointers · sorted array · in place
Keep one copy of each sorted value
Problem
A list is sorted ascending and may contain repeats. Rearrange it in place so its first k positions hold each distinct value once, in order, and return k. What is left after position k does not matter.
Constraints
- 0 ≤ n ≤ 32, sorted ascending
- values from −999 to 999
- O(1) extra space
Examples
[1, 1, 2, 3, 3, 3] → k = 3, first three: [1, 2, 3]
Show the solution
Read and write pointers
a[0..write) holds the distinct values found so far. Because the list is sorted, a value is new exactly when it differs from a[write − 1].
Why it is correct
Invariant: a[0..write) holds each distinct value of a[0..read) once, in order. Sortedness means comparing with the last written value is enough to detect new values.
Complexity
Time O(n), extra space O(1). One pass of the read pointer.
Edge cases
- Empty list: k = 0.
- All equal: k = 1.
- No repeats: every value is copied onto itself.
Common mistakes
- Comparing with a[read − 1] after overwriting it, or with the wrong neighbour.
- Using a hash set, which ignores the sortedness and costs O(n) space.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class RemoveDuplicatesSorted
{
/// <summary>Keeps one copy of each value at the front of a sorted array; returns how many. O(n), O(1) space.</summary>
public static int Apply(int[] values)
{
if (values.Length == 0) return 0;
int write = 1; // values[0..write) holds each distinct value once
for (int read = 1; read < values.Length; read++)
{
if (values[read] != values[write - 1])
{
values[write] = values[read];
write++;
}
}
return write;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/RemoveDuplicatesSorted.cs
def apply(values: list[int]) -> int:
"""Keep one copy of each value at the front of a sorted list; return how many. O(n), O(1) space."""
if not values:
return 0
write = 1 # values[:write] holds each distinct value once
for read in range(1, len(values)):
if values[read] != values[write - 1]:
values[write] = values[read]
write += 1
return write
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/remove_duplicates_sorted.py
package dev.bekadan.dsa.problems;
public final class RemoveDuplicatesSorted {
private RemoveDuplicatesSorted() {
}
/** Keeps one copy of each value at the front of a sorted array; returns how many. O(n), O(1) space. */
public static int apply(int[] values) {
if (values.length == 0) {
return 0;
}
int write = 1; // values[0..write) holds each distinct value once
for (int read = 1; read < values.length; read++) {
if (values[read] != values[write - 1]) {
values[write] = values[read];
write++;
}
}
return write;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/RemoveDuplicatesSorted.java
#pragma once
#include <cstddef>
#include <span>
namespace bekadan::dsa::problems {
// Keeps one copy of each value at the front of a sorted span; returns how many (std::unique does the same). O(n).
inline std::size_t remove_duplicates_sorted(std::span<int> values) {
if (values.empty()) return 0;
std::size_t write = 1; // values[0, write) holds each distinct value once
for (std::size_t read = 1; read < values.size(); ++read) {
if (values[read] != values[write - 1]) {
values[write] = values[read];
++write;
}
}
return write;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/remove_duplicates_sorted.hpp