题目:https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/
12345678910111213141516171819202122
// 方法一:遍历数组,splice删除重复项var removeDuplicates = function(nums) { for(let i = 0; i<nums.length; i++){ let j = i+1 while(nums[i] == nums[j]){ nums.splice(i, 1) //我一直再写j++,其实没必要,因为数据已经被删除了 } }};//方法二:双指针var removeDuplicates = function(nums) { let p=0,q=1 //前后指针 while(q<nums.length){ if(nums[p] != nums[q]){ nums[p+1] = nums[q] p++ } q++ } return p+1};