Remove Duplicates from Sorted Array Two Pointer in DSA C - Time & Space Complexity
We want to understand how the time needed to remove duplicates from a sorted array grows as the array gets bigger.
Specifically, how many steps does the two-pointer method take as the input size increases?
Analyze the time complexity of the following code snippet.
int removeDuplicates(int* nums, int numsSize) {
if (numsSize == 0) return 0;
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 from a sorted array by using two pointers to overwrite duplicates in place.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves pointer
jfrom start to end of the array. - How many times: Exactly once for each element after the first, so
numsSize - 1times.
As the array size grows, the loop runs once for each element, so the steps increase directly with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 checks and possible assignments |
| 100 | About 99 checks and possible assignments |
| 1000 | About 999 checks and possible assignments |
Pattern observation: The number of operations grows linearly as the input size increases.
Time Complexity: O(n)
This means the time to remove duplicates grows in direct proportion to the number of elements in the array.
[X] Wrong: "Because we skip duplicates, the time complexity is less than linear."
[OK] Correct: Even if duplicates are skipped, the loop still checks every element once, so the time grows linearly with input size.
Understanding this linear time solution shows you can efficiently process sorted data with simple pointers, a skill often valued in coding challenges.
"What if the array was not sorted? How would the time complexity change if we still tried to remove duplicates in place?"
Practice
Remove Duplicates from Sorted Array problem?Solution
Step 1: Understand the two-pointer approach
One pointer tracks the position to place the next unique element, the other scans the array.Step 2: Purpose of pointers in-place
This method avoids extra space by overwriting duplicates directly in the original array.Final Answer:
To efficiently track unique elements and overwrite duplicates in-place -> Option AQuick Check:
Two pointers help remove duplicates in-place [OK]
- Thinking two pointers sort the array
- Assuming a new array is created
- Confusing counting duplicates with removing them
i and j for the two-pointer approach in C?Solution
Step 1: Understand pointer roles
Pointeristarts at 0 (first unique),jstarts at 1 (scanning next elements).Step 2: Correct initialization syntax
Both pointers must be initialized properly;int i = 0, j = 1;is correct in C.Final Answer:
int i = 0, j = 1; -> Option AQuick Check:
Initialize i=0 and j=1 for two-pointer [OK]
- Swapping initial values of i and j
- Initializing both pointers to 0
- Incorrect declaration syntax
[1, 1, 2, 3, 3, 4]?Solution
Step 1: Trace the two-pointer process
Start with i=0, j=1. Compare elements: duplicates skipped, unique elements moved forward.Step 2: Count unique elements
Unique elements are 1, 2, 3, 4. So length is 4 after removal.Final Answer:
4 -> Option DQuick Check:
Unique elements count = 4 [OK]
- Counting duplicates as unique
- Assuming length stays same
- Miscounting unique elements
int removeDuplicates(int* nums, int numsSize) {
int i = 0, j = 1;
while (j < numsSize) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
}
j++;
}
return i;
}Solution
Step 1: Analyze return value
Variable i tracks last unique index, so length is i+1, not i.Step 2: Identify correct return
Returning i misses counting the first element, causing off-by-one error.Final Answer:
Returns i instead of i+1 as new length -> Option CQuick Check:
Return length = i+1, not i [OK]
- Off-by-one error in return value
- Ignoring empty array edge case
- Confusing pointer initialization
nums = [0,0,1,1,1,2,2,3,3,4], what will be the array state after applying the two-pointer method to remove duplicates?Solution
Step 1: Apply two-pointer overwrite
Unique elements 0,1,2,3,4 are moved to front positions in array.Step 2: Final array state
Positions after unique elements remain unchanged or ignored (represented as underscores).Final Answer:
[0, 1, 2, 3, 4, _, _, _, _, _] -> Option BQuick Check:
Array front holds unique elements [OK]
- Assuming duplicates remain in place
- Misplacing unique elements
- Confusing array length with content
Ready to prove it?
Pass the test and this pattern moves unknown
