查找出现在所有列表(或数组或集合)中的值

gun*_*win 3 c# algorithm

鉴于以下内容:

List<List<int>> lists = new List<List<int>>();

lists.Add(new List<int>() { 1,2,3,4,5,6,7 });
lists.Add(new List<int>() { 1,2 });
lists.Add(new List<int>() { 1,2,3,4 });
lists.Add(new List<int>() { 1,2,5,6,7 });
Run Code Online (Sandbox Code Playgroud)

确定所有列表中出现哪些数字的最佳/最快方法是什么?

Yae*_*ael 5

您可以使用.net 3.5.Intersect()扩展方法: -

List<int> a = new List<int>() { 1, 2, 3, 4, 5 };
List<int> b = new List<int>() { 0, 4, 8, 12 };

List<int> common = a.Intersect(b).ToList();
Run Code Online (Sandbox Code Playgroud)