Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:44:14+00:00 2026-06-17T19:44:14+00:00

Given two sets of values, I have to find whether there is any common

  • 0

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 C# collection will suit best (in terms of performance) for this purpose? I know that linq has a Intersect extension method to find out the intersection of two list/arrays but my focus is on performance in terms of Big-O notation.

And what if I have to find out the intersection of two sets as well?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:44:15+00:00Added an answer on June 17, 2026 at 7:44 pm

    Well, if you use LINQ’s Intersect method it will build up a HashSet of the second sequence, and then check each element of the first sequence against it. So it’s O(M+N)… and you can use foo.Intersect(bar).Any() to get an early-out.

    Of course, if you store one (either) set in a HashSet<T> to start with, you can just iterate over the other one checking for containment on each step. You’d still need to build the set to start with though.

    Fundamentally you’ve got an O(M+N) problem whatever you do – you’re not going to get cheaper than that (there’s always the possibility that you’ll have to look at every element) and if your hash codes are reasonable, you should be able to achieve that complexity easily. Of course, some solutions may give better constant factors than others… but that’s performance rather than complexity 😉

    EDIT: As noted in the comments, there’s also ISet<T>.Overlaps – if you’ve already got either set with a static type of ISet<T> or a concrete implementation, calling Overlaps makes it clearer what you’re doing. If both of your sets are statically typed as ISet<T>, use larger.Overlaps(smaller) (where larger and smaller are in terms of the size of the set) as I’d expect an implementation of Overlaps to iterate over the argument and check each element against contents of the set you call it on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two arrays, how to find the maximum element which is common to both
Given two sets A and B, what is the common algorithm used to find
Two sets of 'n' numbers are given A and B. Chose one element from
I have two sets of sorted elementes and want to merge them together in
I have two different collections with a common field say, a UserId. There are
Given two sets of values: var subset = new[] { 2, 4, 6, 8
I have two sets of data which I need to join, but there is
Given two sets a = [5,3,4,1,2,6,7] b = [1,2,4,9] c = set(a) - set(b)
Given two tuples of the same arity, how can I lexicographically compare them? It
Given: two images of the same subject matter; the images have the same resolution,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.