# Combinations without Repetition C#

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}
###### Who is Participating?

x

Commented:
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

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

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 Commented:
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

Commented:
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

Author Commented:

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

Commented:
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 Commented:
that is solution.

TommySzalapski thanks a lot
0

Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.