Remove Duplicates from Sorted Array Two Pointer in DSA C# - Time & Space Complexity
We want to know how the time to remove duplicates from a sorted array grows as the array gets bigger.
How many steps does the algorithm take when the input size increases?
Analyze the time complexity of the following code snippet.
public int RemoveDuplicates(int[] nums) {
int i = 0;
for (int j = 1; j < nums.Length; 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
n - 1times wherenis the array length.
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 | 9 |
| 100 | 99 |
| 1000 | 999 |
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 size of the input array.
[X] Wrong: "Because there are two pointers, the time complexity is O(n²)."
[OK] Correct: The two pointers move forward independently but only once through the array, so the total steps are still proportional to n, not n squared.
Understanding this linear time approach shows you can efficiently process arrays without extra space, a skill often needed in real coding tasks.
"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 CQuick 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 problem?Solution
Step 1: Identify pointer roles
Pointeristarts at index 0 to track unique elements,jstarts at 1 to scan ahead.Step 2: Check initialization correctness
Initializingi = 0andj = 1matches the logic of comparing current and next elements.Final Answer:
int i = 0, j = 1; -> Option DQuick Check:
Start i at 0 and j at 1 [OK]
- Starting both pointers at 0
- Swapping i and j initialization
- Starting both pointers at 1
[1, 1, 2, 3, 3]?Solution
Step 1: Trace the two-pointer process
Start with i=0, j=1. Compare elements: skip duplicates, overwrite unique elements at i+1.Step 2: Final array and length
Unique elements are 1, 2, 3 placed at start. Length is 3.Final Answer:
[1, 2, 3], length = 3 -> Option BQuick Check:
Unique elements count = 3 [OK]
- Not updating length after removal
- Leaving duplicates in output
- Misplacing unique elements
int RemoveDuplicates(int[] nums) {
int i = 0, j = 1;
while (j < nums.Length) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
}
j++;
}
return i;
}Solution
Step 1: Understand pointer meaning
Pointer i tracks last unique element index, so length is i + 1.Step 2: Check return value
Returning i alone misses counting the first element, so return i + 1.Final Answer:
The function should return i + 1 instead of i -> Option AQuick Check:
Return length as i + 1 [OK]
- Returning i instead of i + 1
- Incorrect loop boundary causing out-of-range
- Sorting array inside function unnecessarily
nums = [0,0,1,1,1,2,2,3,3,4], what will be the array state and returned length after applying the two-pointer duplicate removal method?Solution
Step 1: Apply two-pointer method step-by-step
Start with i=0, j=1. Move j forward, overwrite duplicates by placing unique elements at i+1.Step 2: Final array and length
Unique elements are 0,1,2,3,4 placed at start. Length is 5.Final Answer:
[0, 1, 2, 3, 4], length = 5 -> Option AQuick Check:
Unique count = 5 [OK]
- Leaving duplicates in the array
- Returning wrong length
- Misplacing unique elements during overwrite
Ready to prove it?
Pass the test and this pattern moves unknown
