# 删除有序数组中的重复项

题目链接: <https://leetcode.cn/problems/remove-duplicates-from-sorted-array/>

解题思路一(用时间换空间):

1. 数组是有序的，那么重复出现的元素都有一个特性，除第一次出现外，后续多次出现的元素都是与前一个元素相同的，可用此方式判定数据重复
2. 仅需要将每个第一次出现的元素按先后顺序从数据头部重新覆盖写入

```go
func removeDuplicates(nums []int) int {
	n := 0
	for _, num := range nums {
		if n == 0 {
			n++
			continue
		} else {
			if nums[n-1] == num {
				continue
			} else {
				nums[n] = num
				n++
			}
		}
	}
	return n
}
```

解题思路二(用空间换时间):

1. 通过map存储出现过的元素，若元素未出现过，则记录并按先后顺序从数组头部覆盖写入
2. 仅需要将每个第一次出现的元素按先后顺序从数据头部重新覆盖写入

```go
func removeDuplicates(nums []int) int {
    numsMap:=map[int]int{}
    n:=0
    for _,num:=range nums{
        if _,has:=numsMap[num];!has{
            nums[n]=num
            n++
            numsMap[num]=1
        }   
    }
    return n
}
```


---

# 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://leetcodebook-1.gitbook.io/top-interview-150/array-string/remove-duplicates-from-sorted-array.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.
