All Possible Combinations of a list of Values
try this:
static void Main(string[] args)
{
GetCombination(new List<int> { 1, 2, 3 });
}
static void GetCombination(List<int> list)
{
double count = Math.Pow(2, list.Count);
for (int i = 1; i <= count - 1; i++)
{
string str = Convert.ToString(i, 2).PadLeft(list.Count, '0');
for (int j = 0; j < str.Length; j++)
{
if (str[j] == '1')
{
Console.Write(list[j]);
}
}
Console.WriteLine();
}
}
Assuming that all items within the initial collection are distinct, we can try using Linq in order to query; let's generalize the solution:
Code:
public static IEnumerable<T[]> Combinations<T>(IEnumerable<T> source) {
if (null == source)
throw new ArgumentNullException(nameof(source));
T[] data = source.ToArray();
return Enumerable
.Range(0, 1 << (data.Length))
.Select(index => data
.Where((v, i) => (index & (1 << i)) != 0)
.ToArray());
}
Demo:
var data = new char[] { 'A', 'B', 'C' };
var result = Combinations(data);
foreach (var item in result)
Console.WriteLine($"[{string.Join(", ", item)}]");
Outcome:
[]
[A]
[B]
[A, B]
[C]
[A, C]
[B, C]
[A, B, C]
If you want to exclude the initial empty array, put .Range(1, (1 << (data.Length)) - 1)
instead of .Range(0, 1 << (data.Length))
Algorithm explanation:
Having a collection of collection.Length
distinct items we get 2 ** collection.Length
combinations (we can compute it as 1 << collection.Length
):
mask - comments
------------------------------------
00..0000 - empty, no items are taken
00..0001 - 1st item taken
00..0010 - 2nd item taken
00..0011 - 1st and 2nd items are taken
00..0100 - 3d item taken
...
11..1111 - all items are taken
To generate all masks we can use direct Enumerable.Range(0, 1 << (data.Length))
Linq query. Now having index
mask we should take item from the collection if and only if corresponding bit within index
is set to 1
:
011001001
^^ ^ ^
take 7, 6, 3, 0-th items from the collection
The code can be
.Select(index => data.Where((v, i) => (index & (1 << i)) != 0)
here for each item (v
) in the collection data
we check if i
-th bit is set in the index
(mask).
Here are two generic solutions for strongly typed lists that will return all unique combinations of list members (if you can solve this with simpler code, I salute you):
// Recursive
public static List<List<T>> GetAllCombos<T>(List<T> list)
{
List<List<T>> result = new List<List<T>>();
// head
result.Add(new List<T>());
result.Last().Add(list[0]);
if (list.Count == 1)
return result;
// tail
List<List<T>> tailCombos = GetAllCombos(list.Skip(1).ToList());
tailCombos.ForEach(combo =>
{
result.Add(new List<T>(combo));
combo.Add(list[0]);
result.Add(new List<T>(combo));
});
return result;
}
// Iterative, using 'i' as bitmask to choose each combo members
public static List<List<T>> GetAllCombos<T>(List<T> list)
{
int comboCount = (int) Math.Pow(2, list.Count) - 1;
List<List<T>> result = new List<List<T>>();
for (int i = 1; i < comboCount + 1; i++)
{
// make each combo here
result.Add(new List<T>());
for (int j = 0; j < list.Count; j++)
{
if ((i >> j) % 2 != 0)
result.Last().Add(list[j]);
}
}
return result;
}
// Example usage
List<List<int>> combos = GetAllCombos(new int[] { 1, 2, 3 }.ToList());