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?

5
  • 2
    the last should be DE not CD Commented Nov 22, 2013 at 15:25
  • 2
    Why did you write CD twice? And where is DE ? Commented Nov 22, 2013 at 15:25
  • dup of stackoverflow.com/questions/1272828/… Commented Nov 22, 2013 at 15:27
  • Can be considered BC as CB? Commented Nov 22, 2013 at 15:29
  • Possible answer here: stackoverflow.com/questions/4319049/… Commented Nov 22, 2013 at 15:29

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

This one's also goof although I think the answer from @jim tollan looks nicer because he doesn't need an extra if statement. :)
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

4 Comments

Seems the only correct attempt so far.. :)
yeah, and i certainly aint no genius - lol. had to think back to the old basic posers from way back. nice brain teaser tho
No, you haven't got downvotes. I can see up/down votes apart per answer and it only says 1up/0down => +1.
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
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!

4 Comments

I upvoted it but later understood that it is incorrect. It will produce both AB and BA which is not wanted.
@sjkm: But it's still the wrong result (20 instead of 10).
You allow AB to be in the result as well as BA, while only AB is expected!
Ah I see, but now it should be right
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

1 Comment

Why don't you run it first? It clearly produces different result than the one asked in the question.
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

3 Comments

You should filter same el pairs, like "AA"
too fast and too inexact :)
And filter out BA etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.