Solved

Combinations without Repetition  C#

Posted on 2013-12-13
9
3,648 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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