Very simple and fast java solution
public List<List<Integer>> subsetsWithDup(int[] nums) {\n        Arrays.sort(nums);\n        List<List<Integer>> res = new ArrayList<>();\n        List<Integer>
    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        Arrays.sort(nums);\n        List<List<Integer>> res = new ArrayList<>();\n        List<Integer> each = new ArrayList<>();\n        helper(res, each, 0, nums);\n        return res;\n    }\n    public void helper(List<List<Integer>> res, List<Integer> each, int pos, int[] n) {\n        if (pos <= n.length) {\n            res.add(each);\n        }\n        int i = pos;\n        while (i < n.length) {\n            each.add(n[i]);\n            helper(res, new ArrayList<>(each), i + 1, n);\n            each.remove(each.size() - 1);\n            i++;\n            while (i < n.length && n[i] == n[i - 1]) {i++;}\n        }\n        return;\n    }\n\n\n----------\nThe Basic idea is: use "while (i < n.length && n[i] == n[i - 1]) {i++;}" to avoid the duplicate. For example, the input is 2 2 2 3 4. Consider the helper function. The process is: \n\n - each.add(n[i]); --> add first 2 (index 0)\n - helper(res, new ArrayList<>(each), i + 1, n); --> go to recursion part, list each is <2 (index 0)>\n - while (i < n.length && n[i] == n[i - 1]) {i++;} --> after this, i == 3, add the element as in subset I
['Java']