> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/backtrack/untitled.md).

# 90. Subsets II

[Given a collection of integers that might contain duplicates, ***nums***, return all possible subsets (the power set).](https://leetcode.com/problems/subsets-ii/)

**Note:** The solution set must not contain duplicate subsets.

```java
class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        // sort array to prevent duplicatin in different recursion layers
        Arrays.sort(nums);
        dfs(nums, res, new ArrayList<>(), 0);
        return res;
    }
    private void dfs(int[] nums, List<List<Integer>> res, List<Integer> list, int start){
        res.add(new ArrayList<>(list));
        for(int i=start; i<nums.length; i++){
            // prevent duplication in the same recursion layer
            // hashset can also be used
            if (i>start && nums[i]==nums[i-1]) continue;
            list.add(nums[i]);
            dfs(nums, res, list, i+1);
            list.remove(list.size()-1);
        }
    }
}
```
