-4

What I want is a slick way to do like

{ A, B, C } ---> { {A, B}, {A, C}, {B, C} }

where A, B and C are of type Widget and { A, B, C } is of type Widget[] and { {A, B}, {A, C}, {B, C} } is of type IEnumerable<Pair<Widget,Widget>> or IEnumerable<Tuple<Widget,Widget>>.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Ms. Corlib
  • 4,993
  • 4
  • 12
  • 19
  • 5
    well, any effort from your side? – sujith karivelil Jul 20 '16 at 00:40
  • sounds like some loops would be good, what algorithm did you use to generate the sample output you gave (a,b a,c b,c) – pm100 Jul 20 '16 at 00:44
  • @drheart All combinations and unique pairs are not the same thing, wouldn't make sense to get all of the combinations and filter if you're just looking for the pairs – konkked Jul 20 '16 at 00:46
  • @konkked More like a partial duplicate, in that if this wasn't clearly a "Gimme teh codez" question, I would actually provide a response. But instead, I encourage the asker to review the possible duplicate for inspiration and either figure it out or come back with a proper question. – CassOnMars Jul 20 '16 at 00:47

1 Answers1

0

Can do with a simple for loop, should give you the pairs you need if you're looking for unique in terms of index

public IEnumerable<IEnumerable<T>> UniquePairs<T>(T[] arr)
{
    for(int i=0;i<arr.Length;i++)
    {
      for(int j=i+1;j<arr.Length;j++)
      {
          yield return new[]{ arr[i],arr[j] };
      }
    }
}
konkked
  • 3,161
  • 14
  • 19