如何检查一个数组是否包含另一个数组的任何项目 [英] How to check if an array contains any item of another array

查看:261
本文介绍了如何检查一个数组是否包含另一个数组的任何项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出2个int数组,例如foobar,最有效的方法是检查数组栏是否包含foo包含的至少一项.应该返回true/false.

Given 2 int arrays e.g, foo and bar, what's the most efficient way to check that the array bar contains at least one item that foo contains. should return true/false.

我怀疑是嵌套的foreach,但只是想知道是否有更好的方法?

I'm suspecting nested foreach but just wondering if there's a nicer way?

推荐答案

使用LINQ:

array1.Intersect(array2).Any()

注意:使用Any()可以确保在找到第一个相等对象时相交算法停止.

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

这篇关于如何检查一个数组是否包含另一个数组的任何项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆