Unique Combinations

This algorithm finds all unique combinations in a specified list of values.



									Public Shared Function GenerateCombinations(arr As String()) As List(Of List(Of String))
	Dim combinations As New List(Of List(Of String))()
	Dim length As Integer = arr.Length

	For i As Integer = 0 To (1 << length) - 1
		Dim combination As New List(Of String)()
		Dim count As Integer = 0

		For count = 0 To length - 1
			If (i And 1 << count) > 0 Then
				combination.Add(arr(count))
			End If
		Next

		If count > 0 AndAlso combination.Count > 0 Then
			combinations.Add(combination)
		End If
	Next

	Return combinations
End Function
								


Example

									Dim arr As String() = {"How", "Are", "You"}
Dim combinations As List(Of List(Of String)) = GenerateCombinations(arr)
								


Output

									{"How"}
{"Are"}
{"How", "Are"}
{"You"}
{"How", "You"}
{"Are", "You"}
{"How", "Are", "You"}