0
0
DSA Pythonprogramming~10 mins

Remove Duplicates from Sorted Array Two Pointer in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Remove Duplicates from Sorted Array Two Pointer
Start with two pointers
Pointer1 at index 0
Compare values at Pointer1 and Pointer2
Repeat until Pointer2 reaches end
Return length = Pointer1 + 1
Use two pointers to scan the array. One pointer tracks unique elements, the other scans ahead to find new unique values to copy forward.
Execution Sample
DSA Python
nums = [1,1,2,2,3]
left = 0
for right in range(1, len(nums)):
    if nums[right] != nums[left]:
        left += 1
        nums[left] = nums[right]
print(left + 1)
This code removes duplicates in-place from a sorted array and returns the new length of unique elements.
Execution Table
StepOperationPointer LeftPointer RightArray StateAction
0Initialize pointers01[1,1,2,2,3]Start with left=0, right=1
1Compare nums[left] and nums[right]01[1,1,2,2,3]1 == 1, duplicate found, move right
2Move right pointer02[1,1,2,2,3]right moves to 2
3Compare nums[left] and nums[right]02[1,1,2,2,3]1 != 2, unique found
4Move left pointer and copy12[1,2,2,2,3]left moves to 1, nums[1] = 2
5Move right pointer13[1,2,2,2,3]right moves to 3
6Compare nums[left] and nums[right]13[1,2,2,2,3]2 == 2, duplicate found, move right
7Move right pointer14[1,2,2,2,3]right moves to 4
8Compare nums[left] and nums[right]14[1,2,2,2,3]2 != 3, unique found
9Move left pointer and copy24[1,2,3,2,3]left moves to 2, nums[2] = 3
10Right pointer reached end24[1,2,3,2,3]End of array reached
11Return length24[1,2,3,2,3]Length = left + 1 = 3
💡 Right pointer reached end of array, all unique elements moved to front
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7After Step 9Final
left0011122
right1123444
nums[1,1,2,2,3][1,1,2,2,3][1,2,2,2,3][1,2,2,2,3][1,2,2,2,3][1,2,3,2,3][1,2,3,2,3]
Key Moments - 3 Insights
Why do we move the left pointer only when we find a new unique element?
Because left pointer marks the position of last unique element. We only advance it to store the next unique value, as shown in steps 4 and 9 in the execution_table.
Why don't we move the left pointer when duplicates are found?
When duplicates are found (steps 1 and 6), we only move the right pointer to skip them. Left pointer stays to overwrite duplicates later with new unique values.
Why is the returned length left + 1?
Because left is zero-based index of last unique element, so length is left + 1 as shown in step 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the array state?
A[1,2,3,2,3]
B[1,2,2,2,3]
C[1,1,2,2,3]
D[1,1,3,2,3]
💡 Hint
Check the 'Array State' column at step 4 in execution_table
At which step does the left pointer move for the second time?
AStep 4
BStep 6
CStep 9
DStep 2
💡 Hint
Look at the 'Pointer Left' column in execution_table and find when it changes the second time
If the input array was [1,1,1,1], what would be the returned length?
A1
B4
C0
D2
💡 Hint
All elements are duplicates except the first, so left pointer stays at 0, length = left + 1
Concept Snapshot
Remove duplicates from sorted array using two pointers:
- left pointer tracks last unique element
- right pointer scans ahead
- if nums[right] != nums[left], move left and copy nums[right]
- continue until right reaches end
- return left + 1 as new length
Full Transcript
This visualization shows how to remove duplicates from a sorted array using two pointers. We start with left pointer at index 0 and right pointer at index 1. We compare values at these pointers. If they are the same, right pointer moves forward to skip duplicates. If different, left pointer moves forward and copies the unique value from right pointer. This process continues until right pointer reaches the end of the array. The final length of unique elements is left pointer index plus one. The array is modified in place to have unique elements at the front.