Solved

# Combinations without Repetition  C#

Posted on 2013-12-13
3,181 Views
1 Endorsement
I need C# example how to make permutation without repetition like:

Variations of {1 2 3} choose 2: RESULT {1 2}, {1 3}, {2 3}

{1 1}, {2, 2} ->not allowed

{1 2} = {2 1}
1
Question by:amatic
• 4
• 3
• 2

LVL 26

Expert Comment

ID: 39716731
If you can put your numbers into an array, you can use Array.Sort(<array>) to put them into order. Then you would want to loop through the array to remove duplicates. From there, it is a matter of looping through the array to get your permutations. Something like:

for(int x = 0; x < <your array>.length; x++)
{
for(int y = 0; y < <your arracy.length; y++)
{
if(x==y) continue;
console.print(string.format("[ {0} {1} ]", x, y));
}
}
0

LVL 26

Expert Comment

ID: 39716744
Microsoft has an example of using the Enumerable.Distinct that would allow you to remove the duplicates:
http://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx
0

Author Comment

ID: 39716868
can we expand the question, what if we have array with ten elements and we're looking for six number of  all combination.

I know that this case have 210 combinatins counting based on this formula ( 10!/(6!*(10-6)!), but ! need list of all combinations

int[] array = new [] {1,2,3,4,5,6,7,8,9,10}

Results will be:
1. {1, 2, 3, 4, 5, 6}
2. {2, 3, 4, 5, 6, 7}
3. {3, 4, 5, 6, 7, 8}
.
.
.
0

LVL 37

Expert Comment

ID: 39717376
One way to do this is to start with 1,2,3,4,5,6 and increase the last number until you cannot increase it any more, then increase the next to last one and reset the rest.
So it would look like
1,2,3,4,5,6
1,2,3,4,5,7
1,2,3,4,5,8
1,2,3,4,5,9
1,2,3,4,5,10 <-- hit the max
1,2,3,4,6,7  <--- 5 goes to 6, then reset last to 7

This can also be modeled in binary
1111110000 represents 1,2,3,4,5,6
1111101000 represents 1,2,3,4,5,7
Move the rightmost 1 one space to the right, if it is at the end, move the next one
So something like
1101100011 would change to
1101011100
0

LVL 37

Accepted Solution

TommySzalapski earned 500 total points
ID: 39717408
Here's an example (tested and working in c++)
int MAX = 10;
int SIZE = 6;
int array[SIZE];
for (int i = 0; i < SIZE; ++i)
{
array[i] = i+1; // initialize array from 1 to SIZE
}
bool done = false;
while (!done)
{
// Show the current one and set up for the next one
// The next two lines just output the array.
// Change them to whatever you need them to be to output the array
for(int i = 0; i < SIZE; ++i) printf("%d, ", array[i]);
printf("\n");
// Find the last element in the array that can be incremented
int index = SIZE-1;
int position_max = MAX;
while(index >= 0 && array[index] == position_max)
{
position_max--;
index--;
}
if (index < 0)
{
done = true;
}
else
{
// increment
array[index]++;
// reset the ones to the right
for(int i = index + 1; i < SIZE; ++i)
{
array[i] = array[i-1]+1;
}
}
}
0

Author Comment

ID: 39717645

TommySzalapski, I made small modification for C# and works perfect for number from 1 to 10, I've got result with 210 combinations.

The problem is that solution is not applicable for any ten numbers, like
int[] array = new int[] { 33, 23, 45, 6, 3, 2, 1, 15, 14, 11 } and now try find all  six number combination form array.

1. {33, 23, 45, 6, 3, 2}
2. {33, 23, 45, 6, 3, 1}
0

LVL 37

Expert Comment

ID: 39717676
You can use the numbers from that code as indexes into the new array.

For example
int[] data_array = new int[] { 33, 23, 45, 6, 3, 2, 1, 15, 14, 11 }

Then change the output array logic to
for(int i = 0; i < SIZE; ++i)
print_to_screen(data_array[array[i]-1]);

The -1 is because we started from 1 in array. If you modified it to use 0-9 instead of 1-10, then you could  skip the -1.
0

Author Comment

ID: 39717729
that is solution.

TommySzalapski thanks a lot
0

LVL 37

Expert Comment

ID: 39718387
I think you messaged me on LinkedIn?
If you have a question for me, just post in one of the topic areas I rank in and I will see it. Topic areas that may be applicable for questions like this one would be Algorithms, Math & Science, or Miscellaneous Programming.
0

## Featured Post

### Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…