Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an array contains any item of another array

Tags:

arrays

c#

People also ask

How do you check if an array contains any element of another array?

Javascript array contains another array To check if the array contains an array in Javascript, use array some(), and array includes() function. The array some() method checks each element against a test method and returns true if any array item passes the test function. Otherwise, it returns false.

How do you find an array within an array?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.


Using LINQ:

array1.Intersect(array2).Any()

Note: Using Any() assures that the intersection algorithm stops when the first equal object is found.


C#3:

bool result = bar.Any(el => foo.Contains(el));

C#4 parallel execution:

bool result = bar.AsParallel().Any(el => foo.AsParallel().Contains(el));

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;
}

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.