Skip to content

Commit

Permalink
Create remove_duplicated_items.md
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Jan 25, 2024
1 parent c93e65c commit 30f0837
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions items/remove_duplicated_items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
description: 删除有序数组中的重复项
link: https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/
rank: 1
---

```golang
func removeDuplicates(nums []int) int {
count := len(nums)
if count <= 1 {
return count
}
duplicated := map[int]struct{}{} // using a map to find the duplicated item easily
for i := 0; i < len(nums); {
num := nums[i]
if _, ok := duplicated[num]; ok {
nums = append(nums[0:i], nums[i+1:]...)
} else {
duplicated[num] = struct{}{}
i++
}
}
return len(nums)
}
```

0 comments on commit 30f0837

Please sign in to comment.