How to check if an array contains any item of another array
Solution 1:
Using LINQ:
array1.Intersect(array2).Any()
Note: Using Any()
assures that the intersection algorithm stops when the first equal object is found.
Solution 2:
C#3:
bool result = bar.Any(el => foo.Contains(el));
C#4 parallel execution:
bool result = bar.AsParallel().Any(el => foo.AsParallel().Contains(el));
Solution 3:
Yes nested loops, although one is hidden:
bool AnyAny(int[] A, int[]B)
{
foreach(int i in A)
if (B.Any(b=> b == i))
return true;
return false;
}
Solution 4:
Another solution:
var result = array1.Any(l2 => array2.Contains(l2)) == true ? "its there": "not there";
If you have class instead of built in datatypes like int etc, then need to override Override Equals
and GetHashCode
implementation for your class.