@zn13621236 Much better than my recursive solution:

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
if (nums == null || nums.length == 0) {
return Collections.<List<Integer>>emptyList();
}
Arrays.sort(nums);
Set<Integer> set = new HashSet<>();
List<List<Integer>> result = new ArrayList<>();
recurse(nums, 0, set, result);
return result;
}
private int recurse(int[] nums,
int index,
Set<Integer> set,
List<List<Integer>> result) {
// base case
if (index == nums.length) {
result.add(new LinkedList<Integer>());
return 1;
}
// recursive case
int delta = recurse(nums, index + 1, set, result);
int finalSize = result.size();
if (set.contains(nums[index])) {
for (int i = finalSize - delta; i < finalSize; i ++) {
LinkedList<Integer> newList = new LinkedList<>(result.get(i));
newList.addFirst(nums[index]);
result.add(newList);
}
return delta;
}
set.add(nums[index]);
for (int i = 0; i < finalSize; i ++) {
LinkedList<Integer> newList = new LinkedList<>(result.get(i));
newList.addFirst(nums[index]);
result.add(newList);
}
return finalSize;
}
}