# VBForums CodeBank > CodeBank - Visual Basic .NET >  Generating unique combinations.

## VBDT

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.

Download the latest version *here*

----------


## strider2005

this looks promising. I have been thinking about doing the same in SQL where input will be set of values from another table. Now that I see you code, I am not sure if this can be done in SQL. I haven't tested your code yet but will this work for numbers? 
So input {1,2,3,4} and we want all unique 3 combinations from list of 4
o/p = {(1,2,3),(1,2,4), (1,3,4), (2,4,3)}

Do you think it is possible to get this kind of output? Thanks for your help.

----------


## strider2005

Thanks for all these examples. I'll try them later today and let you know how it goes.

edit - I tested the code and it rocks!! thanks much.

----------


## JXDOS

thanks for the help. I'll try it out!

----------


## JXDOS

hmm.. there seems to be something wrong when I changed it slightly. When I do it with only numbers 1234 and subset 3.. the list pops up as "(collection)". 

I kept the Combination Class exactly the same but there seems to be a problem when i copied your first example (1234) subset3 and changed it slightly to my own code. I'll post it on when I get back to my computer.

----------


## JXDOS

Numbers = numericupdown
Characters = textbox
Here's the code:


```
Dim com As New Combination
        Dim mylist As New List(Of List(Of Object))
        Dim myobjectlist As New List(Of Object)
        Dim myInt() As Object = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
             For Each i As Object In myInt
            myobjectlist.Add(i)
        Next
        mylist = com.GetCombinations(myobjectlist, Numbers.Value)
        Dim str As String
        ListBox1.Items.Clear()
        For Each i As List(Of Object) In mylist
            str = ""
            For Each j As Integer In i
                str &= j
            Next
            Me.ListBox1.Items.Add(i)
        Next
        For Each i As List(Of Object) In mylist
            i.Sort()
            str = ""
            For Each j As Object In i
                str &= CStr(j)
            Next
            Me.ListBox1.Items.Add(i)
        Next
    End Sub
```

----------


## VBDT

In your code you are assigning a list(Of object) to the ListBox items which is i. You need to use the j you assing the subset numbers to a string and than assing the string to the ListBox item's property.

I updated the class so it will make it easy to sort and all the methods are declared as shared so you dont have to declare an instance of the class. I posted the code examples and it is based on your code.
I hope it helps!  :Smilie:

----------


## JXDOS

there seems to be an error with get subset and sort...
and also.. is there a way to list out all combos with up to 2 repeats of each letter/number? including repeats with different arrangements.

----------


## VBDT

JXDOS the example I posted has no error and I tested it before I posted. What is the error and what is your code. If you are changing the code with out understanding what you are doing than unfurtunatly, there can be many problems. Try the example i posted whith out changing anything and see if it gives you any error. If it does than please provide the error!

----------


## JXDOS

right.. sorry.. it does work fine.. thank you so much for all the help. just one last quesiton.. what about with repeats? like..a certain character could repeat up to x times?

e.g. 

characters = 1,0
repeats = 2
length = 3

output = 110, 001

----------


## VBDT

> right.. sorry.. it does work fine.. thank you so much for all the help. just one last quesiton.. what about with repeats? like..a certain character could repeat up to x times?
> 
> e.g. 
> 
> characters = 1,0
> repeats = 2
> length = 3
> 
> output = 110, 001


The subset size should be lees or equal to the set size otherewize it will throug an exception. You may want to try the multiples or the set objects, it might work {1,1,0,0}

----------


## JXDOS

Thank you very much for all the help!

----------


## mrpb

fantastic...
but whats the alteration i need in this coding is values must be separated 
for instance out put should be
1 2 3 
2 3 4
5 6 3 (with blank spaces )
 or
using any separators...
plssssss i m doing this project for months...
help me  :Smilie:

----------


## k1nda

Hey!

I would like to generate all possible combinations with this code. Does anyone know how to configure the function to do so?

Thanks in advance  :Smilie:

----------


## chrispej

'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
[/HIGHLIGHT]
'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

Hello,
The outcome is exactly what I am look for. I tried the code out and the debugger barks at me at the line:

mySubsets = Combination.GetSubsets(mySet, 3)

with
System.NullReferenceException: 'Object variable or With block variable not set.'

Tried various changes but still the same. I feel like I am missing something simple. Any help would be appreciated.

Regards

----------

