## C# || How To Generate Unique Subsets From Array With Duplicate Values Using C#

The following is a module with functions which demonstrates how to generate unique subsets from an array with duplicate values using C#.

1. Subsets With Dup – Problem Statement

Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:

``` Input: nums = [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] ```

Example 2:

``` Input: nums = [0] Output: [[],[0]] ```

2. Subsets With Dup – Solution

The following is a solution which demonstrates how to generate unique subsets from an array with duplicate values.

``` 2. Subsets With Dup - Solution C# // ============================================================================ // Author: Kenneth Perkins // Date: Oct 27, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to generate unique permutations // ============================================================================ public class Solution { List<IList<int>> result = new List<IList<int>>(); public IList<IList<int>> SubsetsWithDup(int[] nums) { // Sort array Array.Sort(nums); Generate(nums, 0, new List<int>()); return result; } public void Generate(int[] nums, int startIndex, List<int> combination) { result.Add(new List<int>(combination)); for (int index = startIndex; index < nums.Length; ++index) { // Check to see if this number has been visited if (index > startIndex && nums[index] == nums[index - 1]) { continue; } // Add this number to the combination combination.Add(nums[index]); // Keep generating subsets Generate(nums, index + 1, combination); // Remove last item as its already been explored combination.RemoveAt(combination.Count - 1); } } }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637 // ============================================================================//    Author: Kenneth Perkins//    Date:   Oct 27, 2021//    Taken From: http://programmingnotes.org///    File:  Solution.cs//    Description: Demonstrates how to generate unique permutations// ============================================================================public class Solution {    List<IList<int>> result = new List<IList<int>>();     public IList<IList<int>> SubsetsWithDup(int[] nums) {        // Sort array        Array.Sort(nums);        Generate(nums, 0, new List<int>());        return result;    }     public void Generate(int[] nums, int startIndex, List<int> combination) {        result.Add(new List<int>(combination));         for (int index = startIndex; index < nums.Length; ++index) {            // Check to see if this number has been visited            if (index > startIndex && nums[index] == nums[index - 1]) {                continue;            }             // Add this number to the combination            combination.Add(nums[index]);             // Keep generating subsets            Generate(nums, index + 1, combination);             // Remove last item as its already been explored            combination.RemoveAt(combination.Count - 1);        }    }}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

``` [[],[1],[1,2],[1,2,2],[2],[2,2]] [[],[0]] ```

## C# || How To Generate Subsets From Array With Distinct Values Using C#

The following is a module with functions which demonstrates how to generate subsets from an array with distinct values using C#.

1. Subsets – Problem Statement

Given an integer array nums of unique elements, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:

``` Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] ```

Example 2:

``` Input: nums = [0] Output: [[],[0]] ```

2. Subsets – Solution

The following is a solution which demonstrates how to generate subsets from an array with distinct values.

This solution uses bit manipulation to generate 2^n possibilities based of the array length, and then adds items to the result list if the array index is a valid bit based off of the subset possibilities.

``` 2. Subsets - Solution C# // ============================================================================ // Author: Kenneth Perkins // Date: Oct 27, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to generate subsets // ============================================================================ public class Solution { public IList<IList<int>> Subsets(int[] nums) { var result = new List<IList<int>>(); // (2^n possibilities) var possibilities = (1 << nums.Length); // Generate subsets (2^n possibilities) for (int possibility = 0; possibility < possibilities; ++possibility) { var subset = new List<int>(); // Add item to result if index is a valid bit // based off of the subset possibilities for (int index = 0; index < nums.Length; ++index) { if (((possibility >> index) & 1) == 1) { subset.Add(nums[index]); } } result.Add(subset); } return result; } }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031 // ============================================================================//    Author: Kenneth Perkins//    Date:   Oct 27, 2021//    Taken From: http://programmingnotes.org///    File:  Solution.cs//    Description: Demonstrates how to generate subsets// ============================================================================public class Solution {    public IList<IList<int>> Subsets(int[] nums) {        var result = new List<IList<int>>();         // (2^n possibilities)        var possibilities = (1 << nums.Length);         // Generate subsets (2^n possibilities)        for (int possibility = 0; possibility < possibilities; ++possibility) {            var subset = new List<int>();             // Add item to result if index is a valid bit            // based off of the subset possibilities            for (int index = 0; index < nums.Length; ++index) {                if (((possibility >> index) & 1) == 1) {                    subset.Add(nums[index]);                }            }            result.Add(subset);        }         return result;    }}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

``` [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] [[],[0]] ```