> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/442.-find-all-duplicates-in-an-array.md).

# 442. Find All Duplicates in an Array

## 1.問題

![](/files/-LKWuAEvaSoJiVp4FfOr)

## 2.想法

## 3.程式碼

```
class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        vector<int> res;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[abs(nums[i]) - 1] < 0) {
                res.push_back(abs(nums[i]));
            } else {
                nums[abs(nums[i]) - 1] *= -1;
            }
        }
        return res;
    }
};
```

## 4.Performance

![](/files/-LKWuOlXR_cGQEodJEto)
