Remove Duplicates from Sorted Array Two Pointer in DSA Go - 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.
func removeDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}
i := 0
for j := 1; j < len(nums); 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: Single for-loop scanning the array once.
- How many times: Exactly once for each element after the first.
As the array size grows, the loop runs once per element.
| 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 operations grow linearly with input size.
Time Complexity: O(n)
This means the time to remove duplicates grows directly in proportion to the array size.
[X] Wrong: "Since we move elements, it must be slower than just scanning once."
[OK] Correct: The moves happen only when a new unique element is found, so they happen at most n times, keeping the overall time linear.
Understanding this linear time approach shows you can efficiently handle arrays without extra space, a common real-world need.
"What if the array was not sorted? How would the time complexity change?"
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. 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 create a new array
- Assuming sorting is needed inside the method
- Confusing counting duplicates with removing them
nums?Solution
Step 1: Identify pointer roles and correct initialization
Pointer i tracks last unique index starting at 0 (first element), pointer j scans from the next element at 1 to compare adjacent elements.Final Answer:
i, j := 0, 1 -> Option AQuick Check:
i=0, j=1 starts correct two-pointer scan [OK]
- Swapping i and j initialization
- Starting both pointers at 0
- Starting both pointers at 1
nums and the returned length after running the two-pointer duplicate removal on nums := []int{1, 1, 2, 3, 3, 4}?Solution
Step 1: Trace two-pointer process
Start i=0, j=1; compare nums[j] with nums[i]. When different, increment i and set nums[i]=nums[j]. Unique elements placed at start: 1, 2, 3, 4; length = 4; leftover elements remain unchanged. Final array: [1, 2, 3, 4, 3, 4].Final Answer:
nums = [1, 2, 3, 4, 3, 4], length = 4 -> Option DQuick Check:
Unique elements count = 4, array start updated [OK]
- Assuming leftover elements are removed
- Counting duplicates as unique
- Misplacing unique elements
nums:
func removeDuplicates(nums []int) int {
i := 0
for j := 1; j < len(nums); j++ {
if nums[j] != nums[i] {
i = j
nums[i] = nums[j]
}
}
return i + 1
}Solution
Step 1: Analyze pointer update logic
Pointer i should move forward by one (i++) to place unique element, not jump to j (i = j). Correct code uses i++ then nums[i] = nums[j] to overwrite duplicates properly.Final Answer:
Pointer i is incorrectly updated to j instead of incrementing -> Option BQuick Check:
i must increment, not jump to j [OK]
- Setting i = j causes skipping elements
- Starting loop at wrong index
- Wrong return value calculation
nums := []int{0,0,1,1,1,2,2,3,3,4}, what will be the final array state and length after applying the two-pointer duplicate removal method?Solution
Step 1: Apply two-pointer logic step-by-step
Start with i=0, j=1; move j forward, when nums[j] != nums[i], increment i and set nums[i]=nums[j]. Unique elements are 0,1,2,3,4 placed at start; length = 5; rest of array remains unchanged: [0,1,2,3,4,2,2,3,3,4].Final Answer:
nums = [0,1,2,3,4,2,2,3,3,4], length = 5 -> Option CQuick Check:
Unique count 5, array start updated correctly [OK]
- Assuming leftover elements are removed
- Miscounting unique elements
- Overwriting wrong indices
Ready to prove it?
Pass the test and this pattern moves unknown
