Find a lesson, question or project

Start typing to search published content and planned structures.

↑ ↓ moveEnter openEsc close
01 / WHAT IT SOLVES

Removing, moving or deduplicating values without allocating another full collection.

02 / HOW TO RECOGNIZE IT
  • modify the same array
  • preserve relative order
  • return the new valid length
03 / PROTECT THIS INVARIANT

Positions before write contain exactly the valid values encountered so far, in original order.

04 / EXPECTED COST

O(n) time. Typical auxiliary space: O(1).

JAVA STARTING POINT

A template to adapt, not memorize.

Java
int write = 0;
for (int read = 0; read < values.length; read++) {
    if (shouldKeep(values[read])) {
        values[write++] = values[read];
    }
}

Common mistakes

  • Advancing write for rejected values
  • Reading overwritten data
  • Returning the last index instead of the length

PUBLISHED / 03

Learn in this order.

Available articles always appear before the publishing plan.

PLANNED PRACTICE

What comes after the published lessons.

  1. Remove a chosen valuePlanned, not yet published
  2. Keep at most two duplicatesPlanned, not yet published