What is the best way to find common elements from 2 sets?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If your inputs are truly sets, the best way to find common elements is to compute the set intersection. That is the operation designed for exactly this purpose, and most languages provide a built-in version that is both clearer and more efficient than hand-written loops.
Use the Built-In Intersection Operation
The mathematical name for the common elements of two sets is their intersection. In Python, the most direct syntax is a & b:
You can also use the method form:
Both versions express the same idea. If the question is literally about sets, this is almost always the best answer because it matches the abstraction exactly.
Built-in intersection is usually implemented with fast membership checks under the hood, so it is not only concise but also a solid performance default.
Convert First If the Data Is Not Yet a Set
Real data often starts as lists or other iterables rather than actual set objects. If duplicates do not matter, convert them first and then intersect:
This is a good approach when the real question is "which unique values appear in both collections." It is not the right approach if you need duplicate counts or original ordering, because sets discard both.
That distinction matters. Many bugs in "find common elements" code are really misunderstandings about whether the problem is about mathematical sets or about sequences with extra semantics.
Preserve Order Only If You Actually Need It
Set intersection naturally ignores order. If you want the result in the order the values appeared in one source collection, use a set for fast lookup but build the result by scanning the source list.
This returns the shared values in the order they first appear in list_a. It is no longer a pure set operation because order is now part of the requirement, but the lookup is still powered by set membership.
Use Counters If Duplicate Counts Matter
If duplicates matter, you are no longer solving a simple set problem. You are solving a multiset problem. In Python, collections.Counter is a better fit:
This returns each shared value as many times as it appears in both collections at minimum count. That is very different from ordinary set intersection, which would collapse the answer to {2, 4}.
The right question is therefore not just "how do I find common elements," but also "what does common mean for this data model."
The Same Idea Applies Across Languages
Every major language has a built-in or standard-library way to intersect sets.
In Java:
In C#:
The syntax changes, but the principle does not: if the data is set-like, use the set intersection operation instead of recreating it manually.
Common Pitfalls
The biggest mistake is converting to sets when duplicates are actually meaningful. Once you do that, repeated values are gone.
Another issue is expecting a set result to preserve order. Sets are designed around membership, not sequence ordering.
People also write nested loops for data that is already stored as sets. That is usually both slower and less expressive than the built-in operation.
Finally, make sure the elements are valid set members in your language. Hash-based set operations require element types that can participate correctly in hashing and equality checks.
Summary
- The best default way to find common elements from two sets is to compute their intersection.
- In Python, use
a & bora.intersection(b). - Convert list-like data to sets only when uniqueness is the intended behavior.
- If order matters, use set membership for lookup but build the result from the source sequence.
- If duplicates matter, use a multiset-style approach such as
Counterinstead of plain sets.
Related reading
- What is the best way to generate all binary strings of the given length in Python using Recursion?
- What is the best way to get all the divisors of a number?
- What is the best way to get the minimum or maximum value from an Array of numbers?
- What is the best way to implement a rate-limiting algorithm for web requests?
- What is the best way to implement nested dictionaries?
- What is the best way to modify a list in a 'foreach' loop?
- What is the best way to sort a partially ordered list?
- What is the big-O of the function log nk

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.