0

我有 2 个数组。

String[] arrFirst={"a","b","c","d","e"};
String[] arrSecond={"a","b","f","d","g"};
String[] arrThird={"a","f","g","h","e"};

我想要像arrFirst和这样arrSecond的结果,结果是3

对于arrFirstarrThird,结果是2

我发现的所有代码都在比较两个数组并返回它们是否相同。

但我想要的是匹配多少。

我可以做循环方式。

但我认为这将花费太多时间,我想知道是否有更快的方法。

谢谢..

4

3 回答 3

6

您可以使用相交方法。

    String[] arrFirst={"a","b","c","d","e"};
    String[] arrSecond={"a","b","f","d","g"};
    String[] arrThird={"a","f","g","h","e"};

    arrFirst.Intersect(arrSecond).Count(); // 3
    arrFirst.Intersect(arrThird).Count(); //2
于 2012-06-27T06:37:46.617 回答
1
arrFirst.Join(arrSecond,f=>f,s=>s,(f,s)=>f).count();
于 2012-06-27T06:37:56.230 回答
1
arrFirst.Zip(arrSecond, (a, b) => a.Equals(b)).Count(a => a);
于 2012-06-27T06:38:48.720 回答