Bird
0
0
DSA Cprogramming~10 mins

Remove Duplicates from Sorted Array Two Pointer in DSA C - 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 elements at Pointer1 and Pointer2
Repeat until Pointer2 reaches end
Return length = Pointer1 + 1
Use two pointers to scan the array. One tracks unique elements, the other scans ahead to find new unique values.
Execution Sample
DSA C
int removeDuplicates(int* nums, int numsSize) {
    int i = 0;
    for (int j = 1; j < numsSize; j++) {
        if (nums[j] != nums[i]) {
            i++;
            nums[i] = nums[j];
        }
    }
    return i + 1;
}
This code removes duplicates in-place from a sorted array using two pointers and returns the new length.
Execution Table
StepOperationPointer i (unique index)Pointer j (scan index)Array StateAction
0Initialize pointers01[1,1,2,2,3]Start with i=0, j=1
1Compare nums[j] and nums[i]01[1,1,2,2,3]nums[1]=1 == nums[0]=1, duplicate found, move j
2Increment j02[1,1,2,2,3]j moves to 2
3Compare nums[j] and nums[i]02[1,1,2,2,3]nums[2]=2 != nums[0]=1, unique found
4Increment i and copy nums[j]12[1,2,2,2,3]i=1, nums[1]=2
5Increment j13[1,2,2,2,3]j moves to 3
6Compare nums[j] and nums[i]13[1,2,2,2,3]nums[3]=2 == nums[1]=2, duplicate found, move j
7Increment j14[1,2,2,2,3]j moves to 4
8Compare nums[j] and nums[i]14[1,2,2,2,3]nums[4]=3 != nums[1]=2, unique found
9Increment i and copy nums[j]24[1,2,3,2,3]i=2, nums[2]=3
10Increment j25[1,2,3,2,3]j moves beyond array length, stop
11Return new length25[1,2,3,2,3]Length = i + 1 = 3
💡 j reaches 5 which is numsSize, loop ends
Variable Tracker
VariableStartAfter Step 4After Step 9Final
i0122
j1245
nums[1,1,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 increment pointer i only when we find a new unique element?
Because i tracks the position of the last unique element. We only move it forward to store the next unique value, as shown in steps 4 and 9.
Why do we compare nums[j] with nums[i] and not nums[j-1]?
Pointer i always points to the last unique element, so comparing nums[j] with nums[i] ensures we detect duplicates correctly, as seen in steps 3 and 8.
What happens to the elements after the new length?
They remain unchanged but are ignored. The function returns the new length (i+1), so only the first part of the array up to that length is valid, as in step 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4, what is the value of pointer i?
A1
B0
C2
D3
💡 Hint
Check the 'Pointer i (unique index)' column at step 4 in the execution table.
At which step does the pointer j move beyond the array length?
AStep 9
BStep 10
CStep 11
DStep 8
💡 Hint
Look at the 'Pointer j (scan index)' column and the 'Action' description in the execution table.
If the array was [1,1,1,1,1], what would be the final value of i?
A0
B1
C4
D5
💡 Hint
Since all elements are duplicates, i stays at 0 as no new unique elements are found.
Concept Snapshot
Remove duplicates from a sorted array using two pointers:
- i tracks last unique element index
- j scans through array
- When nums[j] != nums[i], increment i and copy nums[j]
- Return i + 1 as new length
- Modifies array in-place without extra space
Full Transcript
This concept uses two pointers to remove duplicates from a sorted array in-place. Pointer i marks the position of the last unique element found. Pointer j scans the array from the second element onward. When nums[j] is different from nums[i], it means a new unique element is found. We increment i and copy nums[j] to nums[i]. This process continues until j reaches the end of the array. The function returns i + 1, which is the count of unique elements. The array is modified so that the first i+1 elements are unique, and duplicates beyond that are ignored.