What if you could clean up long sorted lists in just one quick pass without extra memory?
Why Remove Duplicates from Sorted Array Two Pointer in DSA C++?
Imagine you have a long list of names sorted alphabetically, but some names appear more than once. You want to write them down on a new list without repeating any name.
Doing this by checking each name against all others manually is tiring and slow.
Manually scanning the list and comparing each name to all others takes a lot of time and effort.
It's easy to miss duplicates or accidentally skip some names.
This method is slow especially when the list is very long.
The two-pointer method lets you scan the sorted list just once, moving two markers to find and skip duplicates quickly.
This way, you build a new list of unique names efficiently without extra space.
for (int i = 0; i < n; i++) { bool isDuplicate = false; for (int j = 0; j < i; j++) { if (arr[i] == arr[j]) isDuplicate = true; } if (!isDuplicate) uniqueList.push_back(arr[i]); }
int uniqueIndex = 0; for (int currentIndex = 1; currentIndex < n; currentIndex++) { if (arr[currentIndex] != arr[uniqueIndex]) { uniqueIndex++; arr[uniqueIndex] = arr[currentIndex]; } }
This method makes it easy to remove duplicates from sorted lists quickly and with minimal extra space.
When cleaning up a sorted contact list on your phone, this method helps remove repeated contacts fast without losing order.
Manual duplicate removal is slow and error-prone.
Two-pointer technique scans once and skips duplicates efficiently.
It works best on sorted lists and saves space.
Practice
Solution
Step 1: Understand the two-pointer roles
One pointer tracks the position to place unique elements, the other scans the array.Step 2: Overwrite duplicates in-place
When a new unique element is found, it is placed at the tracked position, replacing duplicates.Final Answer:
To track unique elements and overwrite duplicates in-place -> Option AQuick Check:
Two-pointer technique = overwrite duplicates [OK]
- Thinking it creates a new array instead of modifying in-place
- Assuming it sorts the array again
- Confusing counting duplicates with removing them
nums in C++?Solution
Step 1: Identify pointer roles
Pointer i tracks the last unique element index, starting at 0; j scans from 1.Step 2: Initialize pointers accordingly
Set i = 0 and j = 1 to start comparing from the second element.Final Answer:
int i = 0, j = 1; -> Option AQuick Check:
i=0, j=1 start positions [OK]
- Starting both pointers at 0
- Swapping i and j initialization
- Starting j before i
nums = {1, 1, 2, 3, 3, 4}?Solution
Step 1: Identify unique elements in order
Unique elements are 1, 2, 3, 4.Step 2: Count unique elements
There are 4 unique elements, so the length after removal is 4.Final Answer:
4 -> Option BQuick Check:
Unique count = 4 [OK]
- Counting duplicates as unique
- Confusing length with original array size
- Off-by-one errors in counting
nums:
int removeDuplicates(vector<int>& nums) {
int i = 0, j = 1;
while (j < nums.size()) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
}
j++;
}
return i;
}Solution
Step 1: Understand pointer i meaning
Pointer i is index of last unique element, so length is i + 1.Step 2: Check return statement
Returning i alone gives index, not count; must return i + 1 for length.Final Answer:
Return value should be i + 1, not i -> Option DQuick Check:
Length = last index + 1 [OK]
- Returning i instead of i + 1
- Swapping pointer initializations
- Using wrong comparison operator
nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4}, what will be the state of the array after applying the two-pointer method to remove duplicates, and what is the new length?Solution
Step 1: Identify unique elements in order
Unique elements are 0, 1, 2, 3, 4.Step 2: Overwrite duplicates in-place
Using two pointers, these unique elements overwrite the start of the array.Step 3: Determine new length
There are 5 unique elements, so length is 5.Final Answer:
Array: {0, 1, 2, 3, 4, _, _, _, _, _}, Length: 5 -> Option CQuick Check:
Unique elements = 5, array start updated [OK]
- Not overwriting duplicates correctly
- Miscounting unique elements
- Assuming array length stays same
Ready to prove it?
Pass the test and this pattern moves unknown
