Complete the code to initialize the slow pointer for the two-pointer approach.
func removeDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}
slow := [1]
for fast := 1; fast < len(nums); fast++ {
if nums[fast] != nums[slow] {
slow++
nums[slow] = nums[fast]
}
}
return slow + 1
}The slow pointer starts at index 0 to track the position of the last unique element.
Complete the code to compare elements at fast and slow pointers.
for fast := 1; fast < len(nums); fast++ { if nums[fast] [1] nums[slow] { slow++ nums[slow] = nums[fast] } }
We move the slow pointer only when the fast pointer finds a different element (not equal).
Fix the error in the return statement to return the correct length of unique elements.
return [1] + 1
The slow pointer ends at the last unique element's index, so length is slow + 1.
Fill both blanks to complete the loop that moves the fast pointer and updates the slow pointer.
for [1] := 1; [2] < len(nums); [1]++ { if nums[[1]] != nums[slow] { slow++ nums[slow] = nums[[1]] } }
The fast pointer variable is named 'fast' and used consistently in the loop.
Fill all three blanks to complete the function that removes duplicates from a sorted array.
func removeDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}
[1] := 0
for [2] := 1; [2] < len(nums); [2]++ {
if nums[[2]] != nums[[1]] {
[1]++
nums[[1]] = nums[[2]]
}
}
return [1] + 1
}The slow pointer tracks the last unique element, and fast pointer scans the array.
