# 如何高效地判断两个集合是否有交集

0

artwl | 专家六级 | 园豆：16536

0

var expectedList = list1.Except(list2); 有差集

var intersectedList = list1.Intersect(list2);交集

0

`var A = Enumerable.Range(1, 100).ToList();var B = Enumerable.Range(1, 5).ToList();bool ab = B.Any(a => A.Contains(a));`

0

`List<int> a = new List<int>();            List<int> b = new List<int>();            Random r1 = new Random(), r2 = new Random();            for (int i = 0; i < 100; i++)            {                a.Add(r1.Next(0, 5000));                if (i < 50)                    b.Add(r2.Next(0, 5000));            }            a.AddRange(b);            a.Sort();            bool isHas = false;            for (int i = 0; i < a.Count - 1; i++)            {                if (a[i] == a[i + 1])                {                    isHas = true;                    break;                }            }            Console.WriteLine(string.Format("有无重复：{0}", isHas));            Console.ReadLine();`

0

Klaus.Fenng | 园豆：199 (初学一级) | 2016-05-19 11:14

您需要登录以后才能回答，未注册用户请先注册