Simple way to find if two different lists contain exactly the same elements?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
When comparing two lists to determine if they contain exactly the same elements, a variety of methods and considerations apply, depending on the language and context. This article explores simple, yet effective ways to perform this comparison, catering particularly to dynamic programming languages like Python. While the principles might be relatable across other languages, the examples provided focus mainly on Python due to its widespread use and understanding.
Understanding the Problem
The question at hand is whether two lists contain the identical set of elements regardless of order. This comparison is not merely about the lists having the same elements in the same order (which would be a straightforward comparison) but checks for the same elements in any order. Additionally, it's crucial to consider that the lists might also contain duplicate elements.
Example Lists
- List A:
[1, 3, 5, 2] - List B:
[3, 2, 1, 5] - List C:
[1, 3, 5]
Here, Lists A and B contain the same elements, however, Lists A and C do not, as List C lacks the element 2.
Method 1: Sorting and Then Comparing
One straightforward approach is to sort both lists and then compare them. If they are identical after sorting, it means they contain the same elements in the same frequency.
This method is effective but might not be the most efficient in terms of time complexity, as sorting usually takes time where is the length of the list. The advantage here is the ease of implementation and understanding.
Method 2: Using a Frequency Counter
A more nuanced approach involves counting the frequency of each element in both lists and then comparing these counts. This often involves using a hash table or dictionary in Python.
This method is typically more efficient than sorting for large datasets because operations with hash tables have a constant average-time complexity, . However, there's an added space complexity for storing the counts.
Using Set Operations (With a Caveat)
If duplicates were not a concern (i.e., all elements are unique), a simple comparison utilizing sets would suffice:
Note that this method fails if either list contains duplicates since sets automatically remove any duplicate occurrences.
Table: Summary of Methods
| Method | Handling Duplicates | Performance |
| Sort and Compare | Yes | |
| Frequency Counting | Yes | |
| Set Comparison | No | (but fails with duplicates) |
Factors to Consider
When choosing an approach, consider:
- Input size: Frequency counting is generally faster on large lists.
- Presence of duplicates: Avoid set-based approaches if duplicates are in play.
- Language and Library Support: Simplify with built-in functions like Python’s
Counteror similar.
Conclusion
Fundamentally, the choice of method will heavily depend on the specific requirements and constraints of the task, such as the importance of duplicate value handling and performance necessities. Knowing various methods allows for flexibility and adaptability in solving the problem across different scenarios.
Related reading
- Simpler way of sorting three numbers
- Simplest feature selection algorithm
- Simplify the inverse of Z X X Y function
- Simplifying expression trees
- Simple way to visualize a TensorFlow graph in Jupyter?
- Simpler way to create dictionary of separate variables?
- Simple way to measure cell execution time in ipython notebook
- Simpler way to avoid the UserWarning Converting sparse IndexedSlices

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.