Solved

Combinations without Repetition  C#

Posted on 2013-12-13
9
3,763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
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…

752 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