All possible subsets of distinct integers

This problem involves finding all possible subsets of distinct integers. The solution set must not contain duplicate subsets, and the elements in each subset must be in non-descending order.

Problem

Given a set of distinct integers, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Solution

This solution is in C#. Our A.I. can create solutions in multiple languages.
by kayakane
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterviewPrepQuestions.Backtracking
{
    class Subsets
    {
        public IList<IList<int>> SubsetsWithDup(int[] nums)
        {
            IList<IList<int>> result = new List<IList<int>>();
            Array.Sort(nums);
            Backtrack(result, new List<int>(), nums, 0);
            return result;
        }

        private void Backtrack(IList<IList<int>> result, List<int> tempList, int[] nums, int start)
        {
            result.Add(new List<int>(tempList));
            for(int i = start; i < nums.Length; i++)
            {
                if (i > start && nums[i] == nums[i - 1]) continue; // skip duplicates
                tempList.Add(nums[i]);
                Backtrack(result, temp List, nums, i + 1);
                tempList.RemoveAt(tempList.Count - 1);
            }
        }
    }
}

A.I. Evaluation of the Solution

The candidate's solution correctly generates all possible subsets for a given set of distinct integers. The candidate uses backtracking to generate the subsets. The candidate could improve their solution by adding comments to explain the backtracking approach and how it generates all possible subsets. The candidate could also improve the naming of their variables to make the code more readable.

Evaluated at: 2022-11-21 02:16:10