Skip to content

删除有序数组中的重复项

LeetCode-26

js
// 双指针
let removeDuplicates = function (nums = []) {
  let len = nums.length
  if (!len) return 0
  let fast = 1
  let slow = 0
  while (fast < len)
    if (nums[fast] === nums[slow])
      ++fast
    else
      nums[++slow] = nums[fast]
  return slow + 1
}
let nums = [2, 3, 3, 3, 5, 5, 6, 7]
let res = removeDuplicates(nums, 3)
console.log(res)
console.log(nums)