Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Subsets II problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Subsets II problem solution, we have given an integer array nums that may contain duplicates, and return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order.

Leetcode Subsets II problem solution

Leetcode Subsets II problem solution in Python.

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        ret = set()
        def fn(start, l, tmp):
            if len(tmp) == l:
                ret.add(tuple(tmp))
                return
            
            for i in range(start, len(nums)):
                if i>start and nums[i] == nums[i-1]: continue
                fn(i+1, l, tmp+[nums[i]])
                
        
        for length in range(len(nums)+1):
            fn(0,length,[])
            
        return ret

Subsets II problem solution in Java.

public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] num) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        result.add(new ArrayList());
        
        Arrays.sort(num);
        
        for(int i : num){
            List<List<Integer>> temp = new ArrayList<List<Integer>>();
            for(List<Integer> sub : result){
                List<Integer> inner = new ArrayList<Integer>(sub);
                inner.add(i);
                if(!result.contains(inner))
                    temp.add(inner);
            }
            result.addAll(temp);
        }
        return result;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        
        set<multiset<int>> st;
        
        vector<vector<int>> res;
        
        multiset<int> s;
        
        dfs(st,s, nums, 0);
        
        for(auto x: st){
            res.push_back(vector<int> (x.begin(),x.end()));
        }
        
        return res;
    }
    
    void dfs(set<multiset<int>>& st, multiset<int> s, vector<int> nums, int i){
        
        st.insert(s);
        
        if(i>=nums.size())
            return;
        
        dfs(st,s,nums,i+1);
        
        s.insert(nums[i]);
        
        dfs(st,s,nums,i+1);
        
        
    }
};

Problem solution in C.

void helper(int* nums, int len, int* rsize, int* *rcsizes, int** *ans, int ptr, int _len) {
    if (_len >= len || ptr >= len) return;  

    int prev = *rsize - 1;

    for (int q = ptr; q < len; q++){ 
        if (q > ptr && nums[q] == nums[q-1]) continue;
        (*rcsizes)[*rsize] = _len + 1;
        (*ans)[*rsize] = malloc(sizeof(int) * (*rcsizes)[*rsize]);
        memcpy((*ans)[*rsize], (*ans)[prev], sizeof(int) *_len); 
        (*ans)[*rsize][_len] = nums[q]; 

        (*rsize)++; 

        helper(nums, len, rsize, rcsizes, ans, q + 1, _len + 1); 
    } 
}

int cmp (int *a, int *b){
    return *a - *b;
}

int** subsetsWithDup(int* nums, int len, int* rsize, int* *rcsizes){
	
    qsort(nums, len, sizeof(int), cmp);
    
    *rcsizes = malloc(sizeof(int) * pow(2, len));
    (*rcsizes)[0] = 0;
    
    int* *ans = malloc(sizeof(int*) * pow(2, len));
    
    *rsize = 1; 

    helper(nums, len, rsize, rcsizes, &ans, 0, 0); 

    return ans;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes