Solved

Combinations without Repetition  C#

Posted on 2013-12-13
9
3,289 Views
1 Endorsement
Last Modified: 2013-12-14
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
Comment
Question by:amatic
  • 4
  • 3
  • 2
9 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
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;
        <output your permutations here>
        console.print(string.format("[ {0} {1} ]", x, y));
    }
}

Open in new window

0
 
LVL 26

Expert Comment

by:Shaun Kline
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

by:amatic
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

by:TommySzalapski
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Accepted Solution

by:
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;
         }
      }
   }

Open in new window

0
 

Author Comment

by:amatic
ID: 39717645
thanks all for answers,

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

by:TommySzalapski
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]);

Open in new window


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

by:amatic
ID: 39717729
that is solution.

thanks all for answers,

TommySzalapski thanks a lot
0
 
LVL 37

Expert Comment

by:TommySzalapski
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now