Java | Python | Recursion Visualization
Constraints:\n\n1 <= nums.length <= 10\n-10 <= nums[i] <= 10\n\nthe input size give us a clear hint that it is a backtracking solution, if there were no duplica
Constraints:\n\n**1 <= nums.length <= 10**\n-10 <= nums[i] <= 10\n\nthe input size give us a clear hint that it is a backtracking solution, if there were no duplicates then it would easy problem to backtrack, the trick is to skip the duplicates **except the first time**.\n**backTrack( index, currentList )** \n\nExample -> nums = [1,2,2,3]\n\n![image](https://assets.leetcode.com/users/images/faa81e48-fd2d-4cea-ba2c-6daa7d187343_1628014649.6702619.gif)\n\n\nPython -\n```\nclass Solution:\n    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:\n        result=[]\n        nums.sort()#To handle duplicate first we sort the array ( adjacent elements will be similar )\n        def backtrack(nums,index=0,arr=[]):\n            result.append( arr.copy() )\n            for i in range( index, len(nums)):\n                if i != index and nums[i] ==nums[i-1]: #skip the duplicates, except for the first time\n                    continue\n                arr.append(nums[i]) #include the element\n                backtrack(nums,i+1,arr ) #explore\n                arr.pop() #remove the element\n            \n        backtrack(nums)\n        return result\n```\n\t\t\n\n\nJava version -\n\n```\nclass Solution {\n    List<List<Integer>> output;\n    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        Arrays.sort( nums ); //To handle duplicate first we sort the array ( adjacent elements will be similar )\n        output = new ArrayList();\n        backTracking( 0, nums, new ArrayList() );\n        return output;\n    }\n    \n    public void backTracking( int index, int[] nums, List<Integer> list )\n    {\n        output.add( new ArrayList(list) );\n        for( int i = index;i < nums.length; i++ )\n        {\n            if( i != index && nums[i] == nums[i-1] ) //skip the duplicates, except for the first time\n                continue;\n            list.add( nums[i]); //include\n            backTracking(i+1,nums,list); //explore\n            list.remove( list.size()-1);//backtrack, remove the element\n        }\n    }\n}\n```\n![image](https://assets.leetcode.com/users/images/103350e1-7c5a-4cf1-836f-0af96d3c6359_1627981167.994198.png)\n\nPlease **upvote** if you find it **uselful**. Thanks.\n
['Backtracking', 'Python', 'Java']