BEATS 100%|| IN DEPTH SOLUTION.
Intuition\n Describe your first thoughts on how to solve this problem. \nCore Idea:\n\n- It iterates through the sorted array and for each element, decides whet
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Core Idea:**\n\n- It iterates through the sorted array and for each element, decides whether to include it or exclude it in the current subset being constructed.\n- By recursively exploring both possibilities (including and excluding), it generates all possible unique combinations.\n\n**Key Point: Handling Duplicates**\n\n- Sorting the input array (`nums`) is crucial. It ensures that duplicate elements are grouped together.\n- The duplicate check `if (i > n && nums[i] == nums[i - 1])` avoids adding the same duplicate multiple times to a subset. It only considers the first occurrence of a duplicate in each combination.\n\nBy cleverly combining recursion and backtracking, this code effectively explores all possibilities and avoids generating duplicate subsets due to the sorted nature of the input and the specific checks in place.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n\n\n**Steps:**\n\n1. **Initializing Results:**\n   - `List<List<Integer>> ans = new ArrayList<>()`: Creates an empty list `ans` to store the final results (all possible unique subsets).\n\n2. **Recursive Function (`findAns`):**\n   - `void findAns(int n, int[] nums, List<Integer> al)`: This function does the heavy lifting.\n     - `n`: Current index in the original array (`nums`).\n     - `nums`: The input array of numbers.\n     - `al`: A temporary list to store the current subset being constructed.\n   - `ans.add(new ArrayList<>(al))`: Adds a copy of the current `al` (subset) to the `ans` list. This is done to avoid modifying `al` during backtracking as the function explores different options.\n   - `for (int i = n; i < nums.length; i++)`: Iterates through the elements in the `nums` array starting from the current index `n`. This ensures no element is considered twice due to sorting.\n     - `if (i > n && nums[i] == nums[i - 1])`: This check handles duplicates. It skips adding the current element (`nums[i]`) if it\'s the same as the previous element (`nums[i-1]`) and `i` is not the starting index (`i > n`). This ensures only the first occurrence of a duplicate is included in each combination.\n     - `al.add(nums[i])`: Includes the current element (`nums[i]`) in the temporary list `al`. This represents adding the element to the current subset being explored.\n     - `findAns(i + 1, nums, al)`: Recursive call with an incremented index (`i + 1`). This explores the possibility of including the current element in the subset. It moves to the next element and the modified `al` (with the added element) to continue building subsets that include the current element.\n     - `al.remove(al.size() - 1)`: Backtracking step. Removes the element just added from `al`. This simulates the case of excluding the current element from the subset.\n   - After the loop iterates through all elements from the current index (`n`), the function returns to explore other options through recursion.\n\n3. **Returning the Results:**\n   - The main `subsetsWithDup` function calls `findAns(0, nums, new ArrayList<>())` to start the recursion with an empty temporary list (`al`) and index 0 (`n`).\n   - After all recursive calls are done, the `subsetsWithDup` function returns the `ans` list containing all unique combinations of elements from the sorted `nums` array.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(2^N*K)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(2^N)\n# Code\n```\nclass Solution {\n    List<List<Integer>> ans=new ArrayList<>();\n    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        Arrays.sort(nums);\n        findAns(0,nums,new ArrayList<>());\n        return ans;\n    }\n\n    public void findAns(int n,int[] nums,List<Integer> al){\n        ans.add(new ArrayList<>(al));\n        for(int i=n;i<nums.length;i++){\n            if(i>n && nums[i]==nums[i-1]){\n                continue;\n            }\n            al.add(nums[i]);\n            findAns(i+1,nums,al);\n            al.remove(al.size()-1);\n            \n        }\n        \n    }\n}\n```
['Array', 'Backtracking', 'Java']