# Thread: Finding the size of multiple lists.

1. ## Finding the size of multiple lists.

Hi,

I am writing a Windows Form program, and currently it has 3 Lists. I need to loop through all three, and use each possible combination, so am using three nested for loops.

I found that if the outer loop had less elements than one of the inner loops/lists, some were missed off, as I need to save all the possible combinations of list a + b + c.

This is what I wrote:

Code:
```        public int CalculateOrderToProcessLists()//for three lists
{
int order = 0;

if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() > sListThree.Count())
{
order = 123;
}
else if(sListOne.Count() > sListTwo.Count() && sListTwo.Count() < sListThree.Count() && sListOne.Count() > sListThree.Count())
{
order = 132;
}
else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() > sListThree.Count() && sListOne.Count() > sListThree.Count())
{
order = 213;
}
else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() > sListThree.Count() && sListOne.Count() < sListThree.Count())
{
order = 231;
}
else if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() < sListThree.Count() && sListOne.Count() < sListThree.Count())
{
order = 312;
}
else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() < sListThree.Count())
{
order = 321;
}
else
{
order = 999;
}
return order;
}```
My problem is I would like to be able to have 4 (or more if possible) lists, and to calculate what order to process the lists will be exponentially more complex.

I was wondering if anyone could help me come up with a better way of comparing the sizes of the lists, so that I can use the nested loops of say 4 lists, but being able to use N number of lists would be awesome.

Thanks for any help, it's most appreciated.

2. Originally Posted by Swerve
I found that if the outer loop had less elements than one of the inner loops/lists, some were missed off,
I do not really understand what you mean by this. Looks like bug that you have to fix instead of some tricky workaround you are trying to do with finding longest list and looping through it first.

Show the small code example that illustrates the problem

3. Originally Posted by Swerve
as I need to save all the possible combinations of list a + b + c.
Generating all possible combinations arrays is a known problem and fairly documented in the web. For instance: Print all possible combinations of r elements in a given array of size n. In a nutshell you will move all your lists into a single array (you don't need to calculate order of execution) and find all possible permutations from there.

Solutions like the above, use recursion. A generic algorithm should be something like this:

Code:
```// for an array or set P(a1...aN)

permute(i)
if i == N  output A[N] and end
else
for j = i to N do
swap(A[i], A[j])
permute(i+1)
swap(A[i], A[j])```
Other solutions don't use recursion, which makes them dependent on the number and size of the sets to be processed. But...

Originally Posted by Swerve
but being able to use N number of lists would be awesome.
... because of this, a recursive solution is the best option. Even if you can't afford to move all the lists elements into a single array and end up choosing other type of non recursive solutions (google "find all combination in a jagged array", for instance), you will still need to convert that solution into a recursive function in order to handle the fact you don't know the vertical dimension of your jagged array until runtime.