Subsets II

Recursion FAQs (Medium) Medium

Given an integer array nums, which can have duplicate entries, provide the power set. Duplicate subsets cannot exist in the solution set. Return the answer in any sequence.

Examples:

Input : nums = [1, 2, 2]

Output : [ [ ] , [1] , [1, 2] , [1, 2, 2] , [2] , [2, 2] ]

Input : nums = [1, 2]

Output : [ [ ], [1] , [2] , [1, 2] ]

Input : nums = [1, 3, 3]

Constraints

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10

Hints

  • Sort the input array nums to group duplicate elements together. While generating subsets, skip duplicate elements to avoid generating the same subset multiple times
  • Start with an empty subset. For each element in nums, add it to all existing subsets. To handle duplicates, only extend subsets created in the previous iteration for duplicate elements.

Company Tags

Goldman Sachs OYO Rooms Optum Chewy Zomato Etsy Salesforce Rockstar Games Flipkart Stripe Electronic Arts Uber Deloitte Rakuten Freshworks Zoho Intel Oracle Bungie Walmart Roblox Zynga HCL Technologies AMD Databricks Google Microsoft Amazon Meta Apple Netflix Adobe