The important part of the question is that you need different sets (regardless of order). So, for example, the roll of the bone [1, 2, 1] is equal to the roll of the die [1, 1, 2].
I'm sure there are several ways for this cat, but the first thought that comes to mind is to create an EqualityComparer that will compare the list of bones the way you want, and then use LINQ with Distinct()
.
Here is the EqualityComparer
that takes 2 List<int>
and says that they are equal if all elements are equal (regardless of order):
private class ListComparer : EqualityComparer<List<int>> { public override bool Equals(List<int> x, List<int> y) { if (x.Count != y.Count) return false; x.Sort(); y.Sort(); for (int i = 0; i < x.Count; i++) { if (x[i] != y[i]) return false; } return true; } public override int GetHashCode(List<int> list) { int hc = 0; foreach (var i in list) hc ^= i; return hc; } }
And here is the code that uses it. I use LINQ to create a list of all the combinations ... you can also do this with nested for
loops, but for some reason I like this for some reason:
public static void Main() { var values = new[] { 1,2,3,4,5,6 }; var allCombos = from x in values from y in values from z in values select new List<int>{ x, y, z }; var distinctCombos = allCombos.Distinct(new ListComparer()); Console.WriteLine("#All combos: {0}", allCombos.Count()); Console.WriteLine("#Distinct combos: {0}", distinctCombos.Count()); foreach (var combo in distinctCombos) Console.WriteLine("{0},{1},{2}", combo[0], combo[1], combo[2]); }
Hope this helps!