Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I get all possible combinations of elements out of a list in C#? I have a list, lets call it elementsList. I want to find all the possible combinations with the following conditions out of the list:

- No repeated integers.
- The integers in the combinations must be in chronological order.

Each combination must have 5 integers. So for example, if my elementsList contains the integers 1, 2, 3, 4, 5 and 6 - there are 6 valid combinations:

- 1, 2, 3, 4, 5
- 1, 2, 3, 4, 6
- 1, 2, 3, 5, 6
- 1, 2, 4, 5, 6
- 1, 3, 4, 5, 6
- 2, 3, 4, 5, 6

How do I achieve this result in C#? Thanks in advance.

What I have tried:

I have no problems with writing the code, I'm stuck on the logic. What would the process/steps for the above be? Thanks.
Posted
Updated 17-Aug-19 10:21am
Comments
BillWoodruff 18-Aug-19 2:10am    
Show the code you have now.

":the combinations must be in chronological order" What does this mean ?
Member 14561224 18-Aug-19 11:03am    
Okay, sorry I didn't use the correct term. I meant to say that each integer in a combination must be larger than the previous integer in the combination. For example, 1, 3, 2 is not a valid combination, while 1, 4, 5 is.
BillWoodruff 18-Aug-19 11:10am    
With those constraints, you should have a much easier time creating a solution. cheers, Bill
George Swan 18-Aug-19 2:59am    
Have a look at your example. It is a matrix of 6 rows and 5 columns. Each number is present in 5 of the 6 rows. See how the columns have been filled. The first column is filled with the first number in the series until 5 cells have been filled with that number. Then the column filling continues with the next number. When the column is full, the process moves on to the next column until all 6 numbers have been added 5 times and the matrix is full.

Have a look here: Permutations, Combinations, and Variations using C# Generics[^] it's a complete assembly which does all of it: combinations, permutations ... and it explains itself well, too.
 
Share this answer
 
Comments
BillWoodruff 18-Aug-19 2:07am    
That's an excellent resource, and here's another, more recent: https://www.codeproject.com/Articles/1250925/Permutations-Fast-implementations-and-a-new-indexi
Richard Deeming 22-Aug-19 10:13am    
Also Eric Lippert's excellent multi-part series on combinations[^] and permutations[^]. :)
Member 14561224 18-Aug-19 7:29am    
Thanks for the resource, very informative!
OriginalGriff 18-Aug-19 7:40am    
You're welcome!
Maciej Los 19-Aug-19 3:03am    
5ed!
Though it's not crystal clear from your question but my perception is you want to get all combinations of a single List of int let me share some code that'll give you 720 combinations for
C#
var list = new List<int> { 1, 2, 3, 4, 5, 6 };
var allCombinations = list.Permute().ToList();
var stringList = new List<string>();

foreach (var item in allCombinations)
{
      stringList.Add(string.Join(",", item.Select(n => n.ToString()).ToArray()));
}


So stringList will have all your combinations
1,2,3,4,5,6
1,2,3,4,6,5
............
............

C#
public static class Extensions
{
	public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> sequence)
	{
		if (sequence == null)
		{
			yield break;
		}

		var list = sequence.ToList();

		if (!list.Any())
		{
			yield return Enumerable.Empty<T>();
		}
		else
		{
			var startingElementIndex = 0;
			foreach (var startingElement in list)
		    {
				var remainingItems = list.Where((e, i) => i != startingElementIndex);

				foreach (var permutationOfRemainder in remainingItems.Permute())
				{
					yield return startingElement.Concat(permutationOfRemainder);
				}

				startingElementIndex++;
			}
		}
	}

	private static IEnumerable<T> Concat<T>(this T firstElement, IEnumerable<T> secondSequence)
	{
		yield return firstElement;
		if (secondSequence == null)
		{
			yield break;
		}

		foreach (var item in secondSequence)
		{
			yield return item;
		}
	}
}
 
Share this answer
 
v3
Comments
BillWoodruff 18-Aug-19 2:09am    
I wonder if this could be done with a Stack, rather than with recursion.
Member 14561224 18-Aug-19 7:31am    
Thanks, that's something along the lines. However, what I'm trying to do is get a combination of 5 digits from an integer list. A combination must have no repeated integers, and the order does not matter; 1, 2, 3 is the same as 1, 3, 2. So if a list had the integers 1 to 6, there would be 6 combinations, as mentioned in the OP.
Quote:
I'm stuck on the logic.

This the object of the homework. You have to decipher the logic.
First of all, take a sheet of paper and a pencil, then try to sort out some logic in the example:
- What if list have other numbers?
- What values can have the first number in result list? second? ...
- what was its position in list?
- what if you pick only 1 number? 2? 3?
write the resulting lists.
workout similarities.

This search activity is the base of programming job, as you gain experience, part of the activity is also to check if the logic of the problem is similar to another one where the solving algorithm is known.

If I give you just a full blowup solution, it will be like magic to you and you will learn nothing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900