# LC 442 Find All Duplicates in an Array(M)

Given an array of integers, 1 ≤ a\[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime?

难点：不能用多余存储，而且 时间复杂度是O(n)

## 解题思路

1. 不能用多余存储，则必须考虑利用当前数组传递信息。另外 1 ≤ a\[i] ≤ n 很关键，
2. 解题方法是 把数值设为负数，但扫描时候用 它的绝对值来 忽略 之前的更改。

```java
public List<Integer> findDuplicates(int[] nums) {
    List<Integer> dupList = new ArrayList<Integer>();    
    for(int i = 0; i < nums.length; i++){
        int index = Math.abs(nums[i]) - 1;
        if(nums[index]>0){
            nums[index] = -nums[index];
        }else{ //如果数值已经变负数，表示这是重复的数值
            dupList.add(Math.abs(index + 1));
        }
    }
    return dupList;
}
```
