# 47. 数组中重复的数据

java

```java
import java.util.ArrayList;
import java.util.List;

/**
 * 442. 数组中重复的数据
 *
 * 给定一个整数数组 a，其中1 ≤ a[i] ≤ n （n为数组长度）, 其中有些元素出现两次而其他元素出现一次。
 *
 * 找到所有出现两次的元素。
 *
 * 你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗？
 *
 * 示例：
 *
 * 输入:
 * [4,3,2,7,8,2,3,1]
 *
 * 输出:
 * [2,3]
 *
 */
public class Solution442 {

    /**
     * 使用桶排序的思想（不需要排序）.
     * 这句话是关键：1 ≤ a[i] ≤ n （n为数组长度）,
     * v[i]:桶，下标与nums[i]值对应
     *
     * @param nums
     * @return
     */
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> r = new ArrayList<>();
        int[] v=new int[nums.length+1];
        for (int i = 0; i < nums.length; i++) {
            v[nums[i]]++;
        }
        for(int i=1;i<v.length;i++){
            if(v[i]>=2) r.add(i);
        }
        return r;
    }

    public static void main(String[] args) {
        int[] nums = {
                4, 3, 2, 7, 8, 2, 3, 1
        };
        List<Integer> r = new Solution442().findDuplicates(nums);
        for (Integer i : r) {
            System.out.print(i + " ");
        }
    }
}
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/47-shu-zu-zhong-chong-fu-de-shu-ju.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.
