How to check list A contains any value from list B?

后端 未结 8 691
遇见更好的自我
遇见更好的自我 2021-01-30 08:04

List A:

1, 2, 3, 4

List B:

2, 5

How to check if list A contains any value from list B?

e.g. someth

相关标签:
8条回答
  • 2021-01-30 09:05

    I write a faster method for it can make the small one to set. But I test it in some data that some time it's faster that Intersect but some time Intersect fast that my code.

        public static bool Contain<T>(List<T> a, List<T> b)
        {
            if (a.Count <= 10 && b.Count <= 10)
            {
                return a.Any(b.Contains);
            }
    
            if (a.Count > b.Count)
            {
                return Contain((IEnumerable<T>) b, (IEnumerable<T>) a);
            }
            return Contain((IEnumerable<T>) a, (IEnumerable<T>) b);
        }
    
        public static bool Contain<T>(IEnumerable<T> a, IEnumerable<T> b)
        {
            HashSet<T> j = new HashSet<T>(a);
            return b.Any(j.Contains);
        }
    

    The Intersect calls Set that have not check the second size and this is the Intersect's code.

            Set<TSource> set = new Set<TSource>(comparer);
            foreach (TSource element in second) set.Add(element);
            foreach (TSource element in first)
                if (set.Remove(element)) yield return element;
    

    The difference in two methods is my method use HashSet and check the count and Intersect use set that is faster than HashSet. We dont warry its performance.

    The test :

       static void Main(string[] args)
        {
            var a = Enumerable.Range(0, 100000);
            var b = Enumerable.Range(10000000, 1000);
            var t = new Stopwatch();
            t.Start();
            Repeat(()=> { Contain(a, b); });
            t.Stop();
            Console.WriteLine(t.ElapsedMilliseconds);//490ms
    
            var a1 = Enumerable.Range(0, 100000).ToList();
            var a2 = b.ToList();
            t.Restart();
            Repeat(()=> { Contain(a1, a2); });
            t.Stop();
    
            Console.WriteLine(t.ElapsedMilliseconds);//203ms
    
            t.Restart();
            Repeat(()=>{ a.Intersect(b).Any(); });
            t.Stop();
            Console.WriteLine(t.ElapsedMilliseconds);//190ms
    
            t.Restart();
            Repeat(()=>{ b.Intersect(a).Any(); });
            t.Stop();
            Console.WriteLine(t.ElapsedMilliseconds);//497ms
    
            t.Restart();
            a.Any(b.Contains);
            t.Stop();
            Console.WriteLine(t.ElapsedMilliseconds);//600ms
    
        }
    
        private static void Repeat(Action a)
        {
            for (int i = 0; i < 100; i++)
            {
                a();
            }
        }
    
    0 讨论(0)
  • I use this to count:

    int cnt = 0;
    
    foreach (var lA in listA)
    {
        if (listB.Contains(lA))
        {
            cnt++;
        }
    }
    
    0 讨论(0)
提交回复
热议问题