![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a3ef8067a9a9bd25b91bb34506ab68fb.png)
双指针
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9cef01857b1db2e05e29af855b93f539.gif#pic_center)
package arrayorstring
func RemoveDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}
prevValue := 0
for i := 0; i < len(nums); i++ {
if i != 0 && nums[i] == prevValue {
nums = append(nums[:i], nums[i+1:]...)
i--
}
prevValue = nums[i]
}
return len(nums)
}
func RemoveDuplicates2(nums []int) int {
n := len(nums)
if n == 0 {
return 0
}
slow := 1
for fast := 1; fast < n; fast++ {
if nums[fast] != nums[fast-1] {
nums[slow] = nums[fast]
slow++
}
}
return slow
}
func RemoveDuplicates3(nums []int) []int {
n := len(nums)
slow := 1
fast := 1
for fast < n {
if nums[slow-1] != nums[fast] {
nums[slow] = nums[fast]
slow++
}
fast++
}
return nums[:slow]
}