Note: The solution set must not contain duplicate subsets.
classSolution {publicList<List<Integer>> subsetsWithDup(int[] nums) {List<List<Integer>> res =newArrayList<>();// sort array to prevent duplicatin in different recursion layersArrays.sort(nums);dfs(nums, res,newArrayList<>(),0);return res; }privatevoiddfs(int[] nums,List<List<Integer>> res,List<Integer> list,int start){res.add(newArrayList<>(list));for(int i=start; i<nums.length; i++){// prevent duplication in the same recursion layer// hashset can also be usedif (i>start && nums[i]==nums[i-1]) continue;list.add(nums[i]);dfs(nums, res, list, i+1);list.remove(list.size()-1); } }}