The idea is to maintain a separate list of subsets, **newList**, to keep track of all the newly added subsets at the last step in the for loop. If **num[i] == num[i - 1]**, we don't want to append **num[i]** to all existing subsets but just to the ones that just got added last round to prevent duplication.

For example, we have **num** to be {1, 2, 2}

- For {1}, we have subsets {{ }, {1}}.
- For {1, 2}, we have subsets{{ }, {1}, {2}, {1, 2}}. We just add {2} and {1, 2} to the subsets. So
**newList**is {{2}, {1, 2}}. - For {1, 2, 2}, since 2 == 2, we only append 2 to {2} and {1, 2} in
**newList**respectively, which gives us a new**newList**{{2, 2}, {1, 2, 2}}, and then we add all to the subsets, so the result is {{ }, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2}}.

Please comment if you see something wrong or can be improved. Cheers!

```
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] num) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> subset = new ArrayList<Integer>();
result.add(subset);
if (num.length == 0) {
return result;
}
Arrays.sort(num);
List<List<Integer>> newList = new ArrayList<List<Integer>>();
subset = new ArrayList<Integer>();
subset.add(num[0]);
newList.add(subset);
result.add(subset);
for (int i = 1; i < num.length; i++) {
if (num[i] == num[i - 1]) {
List<List<Integer>> nextList = new ArrayList<List<Integer>>();
for (List<Integer> li : newList) {
subset = new ArrayList<Integer>(li);
subset.add(num[i]);
nextList.add(subset);
}
newList = nextList;
result.addAll(newList);
}
else {
newList.clear();
for (List<Integer> li : result) {
subset = new ArrayList<Integer>(li);
subset.add(num[i]);
newList.add(subset);
}
result.addAll(newList);
}
}
return result;
}
}
```