Remove Duplicates from Sorted Array Two Pointer in DSA Python - Time & Space Complexity
We want to know how fast the two-pointer method removes duplicates from a sorted array.
How does the time needed grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
def remove_duplicates(nums):
if not nums:
return 0
i = 0
for j in range(1, len(nums)):
if nums[j] != nums[i]:
i += 1
nums[i] = nums[j]
return i + 1
This code removes duplicates in-place from a sorted array using two pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One loop that goes through the array once.
- How many times: The loop runs once for each element after the first.
As the array size grows, the loop runs more times, directly proportional to the 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 with input size.
Time Complexity: O(n)
This means the time needed grows in a straight line as the array gets bigger.
[X] Wrong: "Because there are two pointers, the time is doubled or squared."
[OK] Correct: Both pointers move forward through the array only once, so the total work is still proportional to the array size, not squared.
Understanding this linear time approach shows you can efficiently handle arrays without extra space, a common skill interviewers look for.
"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 roles
One pointer tracks the position to place the next unique element, the other scans the array.Step 2: Explain in-place overwrite
When a new unique element is found, it is placed at the first pointer's position, avoiding extra space.Final Answer:
To efficiently track unique elements and overwrite duplicates in-place -> Option DQuick Check:
Two pointers help remove duplicates without extra space [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 Python?Solution
Step 1: Identify pointer roles
Pointeristarts at 0 (position for unique elements),jstarts at 1 (scanning pointer).Step 2: Match initialization
Correct initialization isi, j = 0, 1to start comparing adjacent elements.Final Answer:
i, j = 0, 1 -> Option CQuick Check:
Start pointers at 0 and 1 for two-pointer scan [OK]
- Initializing both pointers at 0
- Swapping pointer start positions
- Using incorrect syntax for multiple assignments
nums = [1, 1, 2, 2, 3]
i = 0
for j in range(1, len(nums)):
if nums[j] != nums[i]:
i += 1
nums[i] = nums[j]
print(nums[:i+1])Solution
Step 1: Trace the loop and pointer updates
Initially, i=0, j scans from 1 to 4. When nums[j] != nums[i], i increments and nums[i] is updated.Step 2: Track array changes
After processing, nums becomes [1, 2, 3, 2, 3], but only first i+1=3 elements are unique.Final Answer:
[1, 2, 3] -> Option BQuick Check:
Unique elements extracted as [1, 2, 3] [OK]
- Printing full array instead of slice
- Not updating i correctly
- Confusing pointer roles
nums = [1, 1, 2, 3, 3]
i = 0
for j in range(1, len(nums)):
if nums[i] == nums[j]:
i += 1
nums[i] = nums[j]
print(nums[:i+1])Solution
Step 1: Analyze the if condition
The code increments i and updates nums[i] only when nums[i] == nums[j], which is incorrect for removing duplicates.Step 2: Correct the condition
It should increment i and update nums[i] when nums[j] != nums[i] to keep unique elements.Final Answer:
The condition should check if nums[j] != nums[i] -> Option AQuick Check:
Use != to detect new unique elements [OK]
- Using == instead of !=
- Starting pointers incorrectly
- Printing wrong slice of array
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 in-place?Solution
Step 1: Apply two-pointer logic on nums
Start with i=0, j scans from 1 to end. Each time nums[j] != nums[i], increment i and set nums[i] = nums[j].Step 2: Track unique elements placement
Unique elements placed at start: 0,1,2,3,4. The array's first 5 elements become [0,1,2,3,4].Final Answer:
[0, 1, 2, 3, 4] -> Option AQuick Check:
Unique sorted elements remain at front [OK]
- Assuming duplicates remain in front
- Not updating i correctly
- Confusing array slicing after operation
Ready to prove it?
Pass the test and this pattern moves unknown
