Some times I update the class and post the updated date if I fined any bugs so it is recommended to check out this thread once a while.
This class can be used to generate unique combinations of objects from a list of objects. The way this class is generating the combinations and the fact that the method’s parameter is an array of objects gives us an advantage of having the objects whit different data types. The only limitation of having the objects with different data types is that it can’t be sorted.
Now, let’s talk about the combinations. As I said, the method returns unique combinations of nested list which is different from all the combinations. For example: let's say we have a list of character objects {A,B,C} and we want to get combinations with 2 characters. The all combinations of the list would be:
AB
BA
AC
CA
BC
CB
As you can see "AB" is the same as "BA", "AC" is the same as "CA", and "BC" is the same as "CB" making total of six combinations. But now let look ate the unique combinations.
AB
AC
BC
There are only three combinations.
I want to point it out that the maximum of the list items count that returned is limited to the max integer "2147483647" due to the fact that indexed data types support integer type for the index parameter. Also, I want to mention that the list objects that will be passed to the method should have unique objects in order to get unique lists of objects. For example if we have a set {A,A,B,C} then the method will return:
AA
AB
AC
AB
AC
BC
That is because each element is treated as a single unique object so if you don’t want it to happen then just remove the repeated objects from the set before passing it in to the method parameter.
Using The Code:
'This will get a list of unique combination subsets of numbers.
vb Code:
'An integer array to hold the set.
Dim mySet() As Object = {1, 2, 3, 4, 5}
'Get the unique combinations of numbers.
Dim mySubsets As New List(Of List(Of Object))
mySubsets = Combination.GetSubsets(mySet, 3)
'Clear the display.
Me.ListBox1.Items.Clear()
'Sort the 2D list of combinations.
Combination.Sort(mySubsets)
Dim str As String
For Each i As List(Of Object) In mySubsets
str = ""
For Each j As Integer In i
'Assign the subset numbers to a string.
str &= j
Next j
'Populate the ListBox.
Me.ListBox1.Items.Add(str)
Next i
'This will get the count of combinations.
vb Code:
Me.TextBox2.Text = CStr(Combination.Count(5, 3))
Set = {1, 2, 3, 4, 5}, Subsets = 3
Outcome:
123
124
125
134
135
145
234
235
245
345
Please feel free to comment or notify me about any problems associated with the class.