Back to All Blogs1 min read9 Sept 2026
DSA Patterns That Actually Matter · Part 1 of 5
Two Pointers: The Pattern Behind Many Array Problems
“Learn same-direction and opposite-direction pointer techniques.”
Two Pointers
Two pointers is a reusable technique where two indices move through an array or string while maintaining an invariant.
function hasPair(nums: number[], target: number) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const sum = nums[left] + nums[right];
if (sum === target) return true;
if (sum < target) {
left++;
} else {
right--;
}
}
return false;
}
Common Uses
- remove duplicates,
- compact arrays,
- move zeroes,
- sliding window,
- partitioning.
Note
Do not memorize “two pointers = left and right.” The real idea is using pointer movement plus an invariant to avoid repeated work.
Finished this article?
Mark it done to update your series progress.