是否有一个函数可以在另外两个数组或列表中找到相同的值?

Is there a function that find same values in another two arrays or lists?

如果我有 [1, 2, 4] 和 [1, 3, 4],
[1, 2, 4] and [1, 3, 4] -> [1, 4](find same value)
我要 return [1, 4](相同值).
有没有类似这个例子的功能?
请帮助我。
谢谢。

可以使用.Intersect()方法,

Produces the set intersection of two sequences.

int[] arr1 = { 1, 2, 4 };
int[] arr2 = { 1, 3, 4 };

var result = arr1.Intersect(arr2);
Console.WriteLine(string.Join("\n", result));

输出:

1
4

注意:结果数组将包含所有不同的数字

.Net Fiddle


替代方式:

如果你也想要重复的元素,那么你可以尝试使用.Where() and .Contains()

的组合
var result = arr1.Where(x => arr2.Contains(x)).ToArray();

.Net Fiddle

使用 LINQ。

ArrayOne.Intersect(ArrayTwo);

Docs