# 442. Find All Duplicates in an Array

## 1.問題

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LKWlLG5z4NrHD4-0Q55%2F-LKWuAEvaSoJiVp4FfOr%2F2018082203.jpg?alt=media\&token=4daa5689-d9cc-4a9e-bae9-c5749c9d22c2)

## 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

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LKWlLG5z4NrHD4-0Q55%2F-LKWuOlXR_cGQEodJEto%2F2018082204.jpg?alt=media\&token=ecf0bcac-bade-4aeb-85f7-9ec11faeb39a)
