1

I had similar questions in the past - I have array with n elements in it, how can I get all combinations of two elements from it, without repeating?

Ie. if array is like this:

var arr1 = new[] { "A", "B", "C", "D", "E"};

... result should be

AB
AC
AD
AE
BC
BD
BE
CD
CE
CD

Can you help me please achieve this?

user198003
  • 11,029
  • 28
  • 94
  • 152

5 Answers5

2

Consider the following Code...

var arr1 = new[] { "A", "B", "C", "D", "E" };
var combinations = new List<string>();
for(int i = 0; i < arr1.Length; i++)
{
    for (int j = i + 1; j < arr1.Length; j++)
    {
        combinations.Add(string.Format("{0}{1}", arr1[i], arr1[j]));
    }
}

Good Luck!

Added screenshot for reference...

enter image description here

gpmurthy
  • 2,397
  • 19
  • 21
  • This one's also goof although I think the answer from @jim tollan looks nicer because he doesn't need an extra if statement. :) – Abbas Nov 22 '13 at 15:50
1
var arr1 = new[] { "A", "B", "C", "D", "E" };
var combinations = new List<string>();
foreach (var i in arr1) {
    foreach (var j in arr1) {
         if((int)i.ToCharArray()[0] < (int)j.ToCharArray()[0]) {
             combinations.Add(i + j);
         }
    }
}

now the combinations list contains all combinations!

sjkm
  • 3,887
  • 2
  • 25
  • 43
1

ok, my feeble attempt:

var myArray = new[] { "A", "B", "C", "D", "E" };
var myCombos = new List<string>();
for (int i = 0; i < myArray.Length; i++)
{
    for (int j = (i + 1); j < myArray.Length; j++)
    {
        myCombos.Add(myArray[i] + myArray[j]);
    }
}

now we have:

AB AC AD AE BC BD BE CD CE DE

jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • Seems the only correct attempt so far.. :) – Abbas Nov 22 '13 at 15:46
  • 1
    yeah, and i certainly aint no genius - lol. had to think back to the old basic posers from way back. nice brain teaser tho – jim tollan Nov 22 '13 at 15:48
  • No, you haven't got downvotes. I can see up/down votes apart per answer and it only says 1up/0down => +1. – Abbas Nov 22 '13 at 15:57
  • paranoid -who's paranoid :-)?!?. must be my friday madness going on. btw, enjoyed your curating of the answers, makes it feel quite *live*. nice one - have a grt weekend Abbas – jim tollan Nov 22 '13 at 16:01
0

Try following (cross join in LINQ)

  var arr1 = new[] { "A", "B", "C", "D", "E"};
  var result = (from i1 in arr1
             from i2 in arr1.Where( i=> i[0]>i1[0])
            select i1+i2).ToArray();

Snippet result on ideone here

AB
AC
AD
AE
BC
BD
BE
CD
CE
DE
Tilak
  • 30,108
  • 19
  • 83
  • 131
0

Maybe this little LINQ query:

var allCombinations = from str1 in arr1
                      from str2 in arr1
                      where str1.CompareTo(str2) < 0
                      select str1 + str2;
Console.Write(string.Join(",", allCombinations));

My last is different, i assume that was a typo in your result: AB,AC,AD,AE,BC,BD,BE,CD,CE,DE

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Why don't you run it first? It clearly produces different result than the one asked in the question. – Andrey Nov 22 '13 at 15:36