Intersection of two sets in most optimized way

前端 未结 2 661
感情败类
感情败类 2020-12-09 03:22

Given two sets of values, I have to find whether there is any common element among them or not i.e. whether their intersection is null or not.

Which of the standard

2条回答
  •  情话喂你
    2020-12-09 03:33

    As mentioned , Applying Any() will give you some performance.

    I tested it on pretty big dataset and it gave 25% improvements.

    Also applying larger.Intersect(smaller) rather than the opposite is very important, in my case, it gave 35% improvements.

    Also ordering the list before applying intersect gave another 7-8%.

    Another thing to keep in mind that depending on the use case you can completely avoid applying intersect.

    For example, for an integer list, if the maximum and minimum are not within the same bounders you don't need to apply intersect since they will never do.

    The same goes for a string list with the same idea applied to first letter.

    Again depending on your case, try as much as possible to find a rule where intersection is impossible to avoid calling it.

提交回复
热议问题