Remove Duplicates from Sorted Array

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Two Pointers — Two Indices, One Rule

Move left/right based on a condition instead of checking all pairs.

int L = 0, R = n - 1;
while (L < R) {
    int sum = a[L] + a[R];
    if (sum == target) { L++; R--; }
    else if (sum < target) L++;
    else R--;
}
Two Pointers

Remove Duplicates from Sorted Array

Easy
LeetCode
4/7
EasyTwo PointersLeetCode #26

Remove Duplicates (sorted)

Slow/fast writer pointer. Return new length.

Examples

Example 1

Input

3
1 1 2

Output

2
1 2
Example 2

Input

10
0 0 1 1 1 2 2 3 3 4

Output

5
0 1 2 3 4

Stuck? Take a nudge

C++17