Daily Archives: October 27, 2021

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.

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.

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]]

C# || How To Generate Next Permutation From Array Using C#

The following is a module with functions which demonstrates how to generate the next permutation from an array using C#.


1. Next Permutation – Problem Statement

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).

The replacement must be in place and use only constant extra memory.

Example 1:


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

Example 2:


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

Example 3:


Input: nums = [1,1,5]
Output: [1,5,1]

Example 4:


Input: nums = [1]
Output: [1]


2. Next Permutation – Solution

The following is a solution which demonstrates how to generate the next permutation from an array

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,3,2]
[1,2,3]
[1,5,1]
[1]

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

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


1. Permute Unique – Problem Statement

Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.

Example 1:


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

Example 2:


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


2. Permute Unique – Solution

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

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,1],[2,1,1]]
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

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

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


1. Permute – Problem Statement

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example 1:


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

Example 2:


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

Example 3:


Input: nums = [1]
Output: [[1]]


2. Permute – Solution

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

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,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
[[0,1],[1,0]]
[[1]]