[Complete Walkthrough With Pictures | Working Explained Behind the Approach]
Don\'t have time to read at all. Read only conclusion part, guaranteed you\'ll be able to write the code by yourself\n\n\n/*\n    Question is exactly similar to
**Don\'t have time to read at all. Read only conclusion part, guaranteed you\'ll be able to write the code by yourself**\n\n```\n/*\n    Question is exactly similar to \'Subset I\' problem with slight modification\n    --------------------------------------------------------------------------\n        + We\'ve to avoid duplicates.\n        \n    Recursion Tree Diagram Including Duplicate Subsets\n    ===================================================\n    \n    NOTE:  T : Included ith indexed\'s element, F : Excluded ith indexed\'s element\n    \n                                  [1, 1, 2]                0th -> indicates making decisions for 0th indexed element\n                           T         /   \\          F\n                +------------------+      +----------------------+\n               /                                                  \\\n             {1}+--+                                                {}__   1st -> indexed\'s decisions\n           T/       \\F                                            /    \\ \n           /         \\                                          T/      \\F\n          {1, 1}       {1}+  (duplicate_subtree) --------->    {1}       {}     2nd -> indexed\'s decisions\n        T/    \\F      T/   \\F                                T/  \\    T /  \\F\n        /      \\      /     \\                                /    \\    /    \\\n     {1, 1, 2} {1, 1} {1, 2} {1}                            {1, 2} {1} {2}   {}     3rd -> elements are exhausted\n     ^                         ^                              ^         ^     ^\n     |                         |                              |         |     |              \n     |                         |                              |         |     |  \n     +-------------------------+                              +---------+     |\n                ^                                               duplicates    ^  \n                |                                               subsets       |         \n                +-------------------------------------------------------------+\n                                        required subsets\n                                        \n    \n    \n    If we look at the above recursion tree diagram, we\'ll found that when we move the right subtree of 0th level\n    i.e. after completing entire left subtree for 0th element. Entire Left sub-tree in this right-subtree, is computed\n    twice which is already calculated in the left subtree of 0th level.\n    \n    Because when we made a decision for 1st element in the first(1st) level to include it in the empty \n    set, at this point look the given array carefully the element at index 1 is equal to the previous index 0.\n    \n    Q: What would happen when made a dfs call to index 1 in the left subtree of 1st level?\n    A: This entire DFS would give us duplicate subsets at the end, when elements are exhausted.\n    \n        Because when made a dfs call to the 0th index in the left subtree of 0th level, all possible\n        subsets for the range of indices [1, 2] (inclusive) are already calculated{look at the left hand side of\n        word \'duplicate_subtree\' in the recursion tree diagram}.\n        \n        So therefore, we can say that making dfs call to the index 1 as the left-subtree of 1st level\n        is nothing but redundent.\n  ======================================================================================================\n  \n  Conclusion [To keep in mind to avoid duplicates]\n  ================================================\n        When we\'re ignoring any ith indexed element[i.e. going to the right subtree(i+1=level) of ith level]/excluding\n        the ith element and moving to made the decisions for (i + 1) elemennt in (i + 1) level.\n        \n            If element_at_(i + 1)_index is equal to element at i(i.e. nums[i + 1] == nums[i]) and\n            element at ith index is excluded then must excluded the element at (i + 1) index as well to\n            avoid duplicates.\n            \n                    Because all the subsets in the range of indices [i + 1, n - 1] is already calculated\n                    when we made a dfs[To include ith element] call to the ith index in the ith level.\n  \n        In simple words, basically we\'ve to exclude the element x iff its previous adjacent\n        element is also x and it\'s previously execluded.\n        \n    \n    Q: The biggest question is that what if two equal elements are at different[NOT adjacent] indices like i & j?\n        where i < j and all possible subsets for the range of indices [j, n - 1] is already calculated\n        for some dfs call(s) previously made to any index i.\n        \n    A: Well!! of course this case could occure but here in the question they stated that you can return the subsets\n        in any order. Hence, to make the equal elements adjacent to each other we would sort the given \'nums\' array.\n            + Sorting will only change the order of subsets in the resultant list but NOT the count.\n            + Sorting will adjust all equal element adjacently.\n*/\n\nclass Solution {\n    \n    //isPreviousElementExcluded : True -> Means previous element is excluded. NOT execlude[included] otherwise.\n    \n    private void generateUniqueSubsets(int[] nums, int idx, List<List<Integer>> subsets,\n                                       List<Integer> runningSubset, boolean isPreviousElementExcluded) {\n        if(idx == nums.length) {\n            subsets.add(new ArrayList<>(runningSubset));\n            return;\n        }\n        \n        //Handling the reverse case of our conclusion\n        if(!isPreviousElementExcluded || (isPreviousElementExcluded && idx > 0 && nums[idx] != nums[idx - 1])) {\n            runningSubset.add(nums[idx]);\n            generateUniqueSubsets(nums, idx + 1, subsets, runningSubset, false);\n            runningSubset.remove(runningSubset.size() - 1);\n        }\n        \n        //Exclude the ith element [Calling to right-subtree]\n        generateUniqueSubsets(nums, idx + 1, subsets, runningSubset, true);\n    }\n    \n    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        Arrays.sort(nums);\n        List<List<Integer>> subsets = new ArrayList<>();\n        generateUniqueSubsets(nums, 0, subsets, new ArrayList<>(), false);\n        return subsets;\n    }\n}\n```\n\n```\nExact same Implementation what I\'ve Discussed in the commented section\n```\n\n```\nclass Solution {\n    \n    private void generateUniqueSubsets(int[] nums, int idx, List<List<Integer>> subsets,\n                                       List<Integer> runningSubset, boolean isPreviousElementIncluded) {\n        if(idx == nums.length) {\n            subsets.add(new ArrayList<>(runningSubset));\n            return;\n        }\n        //Since this part : should always call\n        generateUniqueSubsets(nums, idx + 1, subsets, runningSubset, false);\n\n        if(!isPreviousElementIncluded && nums[idx] == nums[idx - 1]) return; //Don\'t repeat inclusion of \'nums[idx]\'\n        \n        runningSubset.add(nums[idx]);\n        generateUniqueSubsets(nums, idx + 1, subsets, runningSubset, true);\n        runningSubset.remove(runningSubset.size() - 1);\n    }\n    \n    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        Arrays.sort(nums);\n        List<List<Integer>> subsets = new ArrayList<>();\n        generateUniqueSubsets(nums, 0, subsets, new ArrayList<>(), true);\n        return subsets;\n    }\n}\n```
['Java']