[JAVA] Easy and clean code with comments.
Runtime: 3 ms, faster than 42.29% of Java online submissions for Subsets II.\n\nSimilar to 78. Subsets [Only have to make two changes as array contains duplicat
Runtime: 3 ms, faster than 42.29% of Java online submissions for Subsets II.\n\nSimilar to ```78. Subsets``` [Only have to make two changes as array contains duplicates:- 1)Sort the array. 2)Use HashSet to add the subset lists.]\n\n```\n/*\n\nSubsets of an array of length n is (2 to the power n). We will loop i from 0 to (2 to the power n). \nThen take binary form of i and make a subset. As binary form contains 0 or 1 only, so if 1 comes, \nthen add the element from the array in the subset list, else not. In a subset of array, each element\nhas 2 choices, either it will come in the subset, or not come in the subset. So, 1 represents that \nelement  will come in the subset, 0 represents element will not come in the subset. \n\nExample : nums = [1,2,3]\n          Number of subsets = (2 to the power 3) = 8. \n          So, loop from 0 to 7.\n\nLoop (i)     |     Binary form     |     Subset we get\n0            |       000           |       []\n1            |       001           |       [3]\n2            |       010           |       [2]\n3            |       011           |       [2,3]\n4            |       100           |       [1]\n5            |       101           |       [1,3]\n6            |       110           |       [1,2]\n7            |       111           |       [1,2,3]\n\n*/\n\nclass Solution {\n    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        //Sort the nums array.\n        Arrays.sort(nums);\n        //Using HashSet to store subset lists, so that duplicate lists are not added.\n        HashSet<List<Integer>> set = new HashSet<>();\n        int n = nums.length;\n        \n        //total number of subsets = \'2 to the power n\' (1 << n = 1 * (2 to the power n) = 2 to the power n).\n        int numOfSub = 1 << n;\n        \n        //As number of subsets of an array of length n is (2 to the power n), so loop i from 0 till (2 to the power n).\n        for (int i = 0; i < numOfSub; i++) {\n            //child list to store a subset.\n            List<Integer> cl = new ArrayList<>();\n            //Store i in a temp variable as i is controlling outer loop and cannot be messed with.\n            int temp = i;\n            \n            //We are looping backwards, as when we are calculating binary of a number, the first bit that \n\t\t\t//comes after dividing the number with 2 is the LSB(Least Significant Bit) or the last bit of the \n\t\t\t//number\'s binary form. \n\t\t\t//For more clarification :- When calculating binary form of a number we divide it repeatedly by 2 \n\t\t\t//until the remainder becomes 0. After that we write the remainder that has come at every step \n\t\t\t//of division backwards.\n            for (int j = n - 1; j >= 0; j--) {\n                //Taking remainder. (temp & 1) is equal to (temp % 2)\n                int rem = temp & 1;\n                //Then dividing it by 2. (temp >> 1) is equal to (temp / 2).\n                temp = temp >> 1;\n                \n                //If rem is equal to 1, then add nums[j].\n                if (rem == 1) {\n                    cl.add(0, nums[j]);\n                }\n            }\n            \n            //After a subset is formed, add it in the set.\n            set.add(cl);\n        }\n        \n        //Convert HashSet to ArrayList and return it.\n        List<List<Integer>> ml = new ArrayList<>(set);\n        \n        return ml;\n    }\n}\n```
['Bit Manipulation', 'Java']