Remove Duplicates from Sorted Array Two Pointer in DSA Javascript - Time & Space Complexity
We want to understand how the time needed changes when removing duplicates from a sorted array using two pointers.
How does the number of steps grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
function removeDuplicates(nums) {
let i = 0;
for (let j = 1; j < nums.length; j++) {
if (nums[j] !== nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
This code moves through the array once, shifting unique values forward to remove duplicates.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One loop that goes through the array from start to end once.
- How many times: Exactly once for each element after the first, so roughly n-1 times for array size n.
As the array gets bigger, the loop runs once for each element, so the steps grow directly with the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 checks and moves |
| 100 | About 99 checks and moves |
| 1000 | About 999 checks and moves |
Pattern observation: The number of operations grows in a straight line with the input size.
Time Complexity: O(n)
This means the time to remove duplicates grows directly with the number of elements in the array.
[X] Wrong: "Because there are two pointers, the time complexity is O(n²)."
[OK] Correct: Both pointers move forward only, never backward, so the loop runs just once through the array, making it O(n), not O(n²).
Understanding this pattern helps you solve many array problems efficiently by using two pointers to avoid extra loops.
"What if the array was not sorted? How would the time complexity change if we still tried to remove duplicates this way?"
Practice
Solution
Step 1: Understand the two-pointer technique
One pointer tracks the position to place the next unique element, the other scans the array.Step 2: Purpose of the technique
It overwrites duplicates by placing unique elements at the front, avoiding extra space.Final Answer:
To efficiently overwrite duplicates and keep unique elements at the start -> Option DQuick Check:
Two-pointer = overwrite duplicates [OK]
- Thinking it sorts the array again
- Confusing counting duplicates with removing them
- Assuming it reverses the array
nums in JavaScript?Solution
Step 1: Identify pointer roles
Pointer i tracks last unique element index, j scans from next element.Step 2: Correct initialization
i starts at 0 (first element), j starts at 1 (second element) to compare.Final Answer:
let i = 0, j = 1; -> Option BQuick Check:
i=0, j=1 start positions [OK]
- Starting both pointers at 0
- Swapping i and j initialization
- Starting both pointers at 1
nums = [1, 1, 2, 2, 3]?Solution
Step 1: Traverse and compare elements
Start with i=0, j=1. Compare nums[j] with nums[i]. If different, increment i and copy nums[j].Step 2: Update array with unique elements
After processing, unique elements at start are [1, 2, 3].Final Answer:
[1, 2, 3] -> Option AQuick Check:
Unique sorted elements = [1, 2, 3] [OK]
- Leaving duplicates in the output
- Mixing order of elements
- Not updating the array in place
nums. What is the bug?
let i = 0;
for (let j = 1; j < nums.length; j++) {
if (nums[j] !== nums[i]) {
i = j;
nums[i] = nums[j];
}
}
return i + 1;Solution
Step 1: Analyze pointer update
i should move forward by incrementing to next unique position, not jump to j.Step 2: Correct pointer increment
Use i++ to move i forward, then assign nums[i] = nums[j].Final Answer:
Pointer i should be incremented, not assigned j -> Option AQuick Check:
i++ to move pointer forward [OK]
- Assigning i = j instead of i++
- Starting loop at wrong index
- Wrong comparison operator
nums = [0,0,1,1,1,2,2,3,3,4], what will be the length of the array after removing duplicates using the two-pointer method, and what will the first part of the array look like?Solution
Step 1: Apply two-pointer method on nums
Traverse array, move unique elements forward: 0,1,2,3,4.Step 2: Count unique elements and array start
There are 5 unique elements, so length is 5, and array start is [0,1,2,3,4].Final Answer:
Length: 5, Array start: [0, 1, 2, 3, 4] -> Option CQuick Check:
Unique count = 5, array front = [0,1,2,3,4] [OK]
- Counting duplicates as unique
- Not updating array in place
- Miscounting length after removal
Ready to prove it?
Pass the test and this pattern moves unknown
