# 移除有序数组中重复项

```
给你一个有序数组 nums ，请你 原地 删除重复出现的元素，使每个元素 最多出现两次 ，返回删除后数组的新长度。
不要使用额外的数组空间，你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
```

```javascript
var removeDuplicates = function (nums) {
    let k = 2;
    return process(nums, k)
}

function process(nums, k) {
    let ans = 0;
    let len = nums.length;
    if (len < 2) return len;

    for (let i = 0; i < len; i++) {
        if (ans < k || nums[ans - k] !== nums[i]) {
            nums[ans++] = nums[i]
        }
    }
    return ans
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://matthrew.gitbook.io/oliver/yi-chu-you-xu-shu-zu-zhong-chong-fu-xiang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
