↶ Home

3Sum dedup.

Why the duplicate-skip works — step through both algorithms side by side.
INTERACTIVE · CLICK NEXT
LC 15
Sorted input
Anchor (this round)
i = 0 · value -1
Inner two-pointer searches L+R = 1 (to cancel the -1 anchor).
Goal
find all unique triplets
anchor (i)
L pointer
R pointer
match found
duplicate
skip in progress
Step 0 / 0

Without skip.

naive · finds duplicates
step
L · R
sum
Press Next → to begin.
triplets recorded
— none yet —

With skip.

canonical · no duplicates
step
L · R
sum
Press Next → to begin.
triplets recorded
— none yet —

The takeaway.

Without skip: when the two pointers land on duplicate values after a match, they emit the same triplet again. The set silently drops it, but the CPU still did the work.

With skip: after a match, we walk L past any equal neighbors (look-ahead: nums[L] == nums[L+1]) and walk R past its equal neighbors (look-behind: nums[R] == nums[R-1]). Then we advance both into fresh territory.

Net effect: no duplicate triplet is ever even reached. No set needed. Result is deterministic in order.

SEE ALSO → Flashcards · two-pointers category · Cheatsheet · pattern 2 · Practice script · LC 15 · LC 16