Bird
0
0
DSA Cprogramming

Remove Duplicates from Sorted Array Two Pointer in DSA C

Choose your learning style9 modes available
Mental Model
Use two pointers to keep track of unique elements and overwrite duplicates in place.
Analogy: Imagine sorting your bookshelf and moving only unique books forward, skipping duplicates as you go.
[1, 1, 2, 2, 3, 3, 4]
 ↑  ↑
left right
Dry Run Walkthrough
Input: array: [1, 1, 2, 2, 3], remove duplicates in place
Goal: Keep only unique elements at the start of the array and find the new length
Step 1: Initialize left at index 0, right at index 1
[1, 1, 2, 2, 3]
 ↑  ↑
left right
Why: Left points to last unique element, right scans for new unique
Step 2: Compare array[right] with array[left], both 1, duplicate found, move right forward
[1, 1, 2, 2, 3]
 ↑     ↑
left   right
Why: Skip duplicate by moving right pointer
Step 3: array[right] = 2 differs from array[left] = 1, increment left and copy 2 to left
[1, 2, 2, 2, 3]
    ↑  ↑
   left right
Why: Found new unique, move left forward and overwrite
Step 4: Compare array[right] = 2 with array[left] = 2, duplicate, move right forward
[1, 2, 2, 2, 3]
    ↑     ↑
   left   right
Why: Skip duplicate by moving right pointer
Step 5: array[right] = 3 differs from array[left] = 2, increment left and copy 3
[1, 2, 3, 2, 3]
       ↑  ↑
      left right
Why: Found new unique, move left forward and overwrite
Step 6: Right pointer reached end, stop
[1, 2, 3, 2, 3]
       ↑
      left
Why: All unique elements processed
Result:
[1, 2, 3, 2, 3]
Unique length = 3
Annotated Code
DSA C
#include <stdio.h>

int removeDuplicates(int* nums, int numsSize) {
    if (numsSize == 0) return 0;
    int left = 0;
    for (int right = 1; right < numsSize; right++) {
        if (nums[right] != nums[left]) {
            left++;
            nums[left] = nums[right];
        }
    }
    return left + 1;
}

int main() {
    int nums[] = {1, 1, 2, 2, 3};
    int size = sizeof(nums) / sizeof(nums[0]);
    int newLength = removeDuplicates(nums, size);
    for (int i = 0; i < newLength; i++) {
        printf("%d", nums[i]);
        if (i < newLength - 1) printf(" -> ");
    }
    printf(" -> null\n");
    printf("Unique length = %d\n", newLength);
    return 0;
}
if (numsSize == 0) return 0;
handle empty array edge case
int left = 0;
initialize left pointer to track last unique element
for (int right = 1; right < numsSize; right++) {
right pointer scans through array
if (nums[right] != nums[left]) {
check if current element is new unique
left++;
move left pointer forward to next unique position
nums[left] = nums[right];
overwrite duplicate with new unique element
return left + 1;
return count of unique elements
OutputSuccess
1 -> 2 -> 3 -> null Unique length = 3
Complexity Analysis
Time: O(n) because we scan the array once with two pointers
Space: O(1) because we modify the array in place without extra space
vs Alternative: Naive approach uses extra space to store unique elements, increasing space to O(n)
Edge Cases
empty array
returns 0 immediately, no processing
DSA C
if (numsSize == 0) return 0;
array with all duplicates
returns 1, only one unique element kept
DSA C
if (nums[right] != nums[left]) {
array with no duplicates
returns original length, array unchanged
DSA C
if (nums[right] != nums[left]) {
When to Use This Pattern
When you see a sorted array and need to remove duplicates in place, use two pointers to overwrite duplicates efficiently.
Common Mistakes
Mistake: Starting left pointer at -1 or forgetting to initialize it properly
Fix: Initialize left pointer at 0 to track the first unique element
Mistake: Copying elements even when they are duplicates
Fix: Only copy when nums[right] != nums[left]
Mistake: Returning left instead of left + 1 as the new length
Fix: Return left + 1 because left is index-based
Summary
Removes duplicates from a sorted array by overwriting duplicates in place.
Use when you want to keep only unique elements without extra space.
Two pointers track unique elements and overwrite duplicates efficiently.