- Home /
Combination of a list - no repeats, specify length
I've been spending quite some time on a problem that I hoped would be fairly straightforward.
I want to populate a list of integers based on the combinations of another list of integers. There can't be any repeats and I want to be able to specify the length of combinations, for example:
List: {0, 1, 2}
Combinations with a length of 1: {0}, {1}, {2}
Combinations with a length of 2: {0, 1}, {0, 2}, {1, 2}
Combinations with a length of 3: {0, 1, 2}
The original list may not always have a length of 3, I just used that as an easy example. Could someone please help me with a way to do this, in C#? I've found numerous examples online, but they either use NET functions which I can't use in Unity, or don't do one of the things I want (no repeats, specify length).
Thanks!
Answer by Bunny83 · Oct 12, 2017 at 01:48 AM
That's actually quite simple. All you need to do is using as many nested for loops as you want numbers in your combination and let the inner loops start at the current outer loop +1. The easiest way would probably be a recursive method if you want to have the combination length variable.
I quickly created this helper class:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Combinations<T>
{
private List<T> m_Items;
private List<List<T>> m_Result;
private T[] current;
private int m_Length;
private Combinations(List<T> aItems, int aLength)
{
m_Items = aItems;
m_Length = aLength;
m_Result = new List<List<T>>();
current = new T[aLength];
}
public static List<List<T>> GetCombinations(List<T> aItems, int aLength)
{
if (aItems == null || aItems.Count < aLength)
return new List<List<T>>();
var context = new Combinations<T>(aItems, aLength);
context.GetCombinations(0, 0);
return context.m_Result;
}
private void GetCombinations(int aStart, int aDepth)
{
if (aDepth >= m_Length)
return;
int c = m_Items.Count + aDepth - m_Length + 1;
for (int i = aStart; i < c; i++)
{
current[aDepth] = m_Items[i];
if (aDepth == m_Length-1)
m_Result.Add(current.ToList());
else
GetCombinations(i+1, aDepth + 1);
}
}
}
You can use it like this:
List<int> numbers = new List<int> {2,3,5,7,11,13,17,23};
List<List<int>> combinations = Combinations<T>.GetCombinations(numbers, 2);
here combinations will contain all combinations with 2 elements of the given list. I've actually tested it with the following code. If does actually print all combinations. The two foreach loops are just to convert the lists into a nice printable form.
private void Start()
{
var n = new List<int> { 1,2,3,4,5,6,7,8,9,10};
var sb = new StringBuilder();
for(int i = 1; i < 11; i++)
{
var c = Combinations<int>.GetCombinations(n, i);
sb.Length = 0;
foreach(var k in c)
{
sb.Append('[');
foreach(int v in k)
{
sb.Append(v).Append(", ");
}
sb.Remove(sb.Length-2, 2);
sb.Append("], ");
}
sb.Remove(sb.Length - 2, 2);
Debug.Log("Combinations of length "+ i + ": " + c.Count + "\n"+ sb.ToString());
}
}
This is beautiful! Thanks so much for taking the time to help. Having tested it it looks to be exactly what I want. I've also compared results against an online combination calculator and results are identical.
Thanks again.