2

Say I've got an

Array1 [1,2,3]

and a List of arrays Array2 [3,2,4] Array3 [2,16,5] I need to return only those elements of the List which contain exactly two ints from Array1. In this case, Array2 since integers 2 and 3 intersect; Thanks

Nikita Rostov
  • 29
  • 1
  • 4

2 Answers2

1

Try to combine Where() and Count():

var matches = new int[] { 1, 2, 3 };
var data = new List<int[]>
{
     new int[] { 3, 2, 4 },
     new int[] { 2, 16, 5 }
};

var result = data.Where(x => x.Count(matches.Contains) == 2);
haim770
  • 48,394
  • 7
  • 105
  • 133
1

since it's int[] you can use the .Intersect() directly. For example

from a in arrays where a.Intersect(Array1).Count() == 2 select a
//arrays contains Array2 and Array3
AD.Net
  • 13,352
  • 2
  • 28
  • 47