判断两个集合是否有交集,集合元素是整型,有没有高效方便的集合类

解决方案 »

  1.   

    有LINQ,比如這樣:
    List<int> left = new List<int>() { 1, 3, 5, 7, 9 };
    List<int> right = new List<int>() { 0, 2, 4, 5, 6, 8 };
    bool result = left.Intersect(right).Count() > 0;
      

  2.   

    msdn的示例:
    'code=C#'
     int[] id1 = { 44, 26, 92, 30, 71, 38 };
                int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };            IEnumerable<int> both = id1.Intersect(id2);            foreach (int id in both)
                    Console.WriteLine(id);'/code'