-1

I have a list of strings

List<String> points = new List<String>();

Let's say points contains [A,B,C,D,E]. I want all possible pairs from the list. My expected output is `

[A,B],
[A,C],
[A,D],
[A,E],
[B,A],
[B,C],
[B,D],
[B,E],
[C,A],
[C,B],
[C,D],
[C,E],
[D,A],
[D,B],
[D,C],
[D,E],
[E,A],
[E,B],
[E,C],
[E,D]

I need to get all the 20 combinations. Can you please help me with this in c#.

wazz
  • 4,953
  • 5
  • 20
  • 34
Sree
  • 973
  • 2
  • 14
  • 32
  • 3
    Let us know [what you have tried so far](https://meta.stackoverflow.com/questions/261592) and what didn't work. For further information, please refer to the help article regarding [how to ask good questions](https://stackoverflow.com/help/how-to-ask). – wazz Jul 27 '19 at 22:59

2 Answers2

2

I decided to go LINQ (because I can and it's fun):

public static void Main (string[] args) {
        List<String> points = new List<String> {"A","B", "C", "D", "E"};

        var perm = (from i in points.Select((Value, Index) => new { Value, Index })
                   from j in points.Select((Value, Index) => new { Value, Index })
                   where i.Index != j.Index
                   select (i.Value, j.Value)).ToList();

        foreach(var item in perm)
        {
            Console.WriteLine($"[{item.Item1},{item.Item2}]");
        }    
    }

Output:

[A,B]
[A,C]
[A,D]
[A,E]
[B,A]
[B,C]
[B,D]
[B,E]
[C,A]
[C,B]
[C,D]
[C,E]
[D,A]
[D,B]
[D,C]
[D,E]
[E,A]
[E,B]
[E,C]
[E,D]

AAA
  • 3,520
  • 1
  • 15
  • 31
  • What should be output if the array is {"A","B", "C", "A", "E"}. Should the combination of A in index 0 and A in index 3 be considered a valid one ? – Anu Viswan Jul 27 '19 at 23:48
  • @Anu, hmmm, Thank you. That depends on the OP's definition: are repeated values permitted in the list, and if so, what's the difference between BA (index 0) and BA(index 3). – AAA Jul 27 '19 at 23:52
  • 1
    right. Just wondering if instead of i!=j, would it be better if the index is considered. For example points.SelectMany((lhs,lhsIndex)=> points .Where((rhs,rhsIndex)=>rhsIndex!=lhsIndex).Select(rhs=>new {lhs,rhs})); – Anu Viswan Jul 27 '19 at 23:53
  • 1
    @AnuViswan, thanks a ton! I have learned from that. I starred this so I would come back to use that code. I just feel so sleepy right now. – AAA Jul 27 '19 at 23:59
1

The first thought that comes to my mind is to use ValueTuples and simple loops as you can see below:

void Main()
{
   List<string> strings = new List<string> () {"A", "B", "C", "D", "E"};

   foreach (var item in OutputCombinations(strings))
   {
      Console.WriteLine($"[{item.Item1}{item.Item2}]");
   }
}

And method for combinations:

public static List<ValueTuple<string, string>> OutputCombinations(List<string> list)
{
   List<ValueTuple<string, string>> pairs = new List<ValueTuple<string, string>>();

   ValueTuple<string, string> currentPair = new ValueTuple<string, string>();
   for (int i = 0; i < list.Count; i++)
   {
       currentPair.Item1 = list[i];

       for (int j = 0; j < list.Count; j++)
       {
           if (i != j)
           {
            currentPair.Item2 = list[j];

            pairs.Add(currentPair);
           }
       }
   }

return pairs;
}

Output:

[AB]
[AC]
[AD]  
[AE]
[BA]
[BC]
[BD]
[BE]
[CA]
[CB]
[CD]
[CE]
[DA]
[DB]
[DC]
[DE]
[EA]
[EB]
[EC]
[ED]
pablocity
  • 484
  • 4
  • 14