Find Duplicate Elements in a Python List
Identifies and returns duplicate elements from a Python list using sets for efficient membership tests.
Python code
13 linesdef find_duplicates(lst):
seen = set()
duplicates = set()
for item in lst:
if item in seen:
duplicates.add(item)
else:
seen.add(item)
return list(duplicates)
if __name__ == "__main__":
sample = [1, 2, 3, 2, 4, 1, 5, 3]
print(find_duplicates(sample))
Output
[1, 3, 2]
How it works
This function uses two sets: seen tracks items already encountered, and duplicates stores items that appear more than once. For each element, if it's already in seen, it's added to duplicates; otherwise, it's added to seen. Since sets ignore ordering, the output order may vary between runs. The solution runs in O(n) time on average.
Common mistakes
- Using a list to track seen items, which makes the function O(n^2).
- Not converting the result to a list, leading to an unordered set output.
- Counting occurrences and then filtering, which is less efficient for large lists.
Variations
- Use a dictionary to count occurrences and then filter keys with count > 1.
- Use a list comprehension with counting but note it is slower for large data.
Real-world use cases
- Detecting duplicate user IDs in a dataset before syncing to a CRM.
- Finding repeated error codes in a log file to highlight frequent issues.
- Identifying duplicate SKU entries in an inventory CSV before import.
Sponsored
More from Lists & loops
- Check if List is Sorted Ascending in Python easy
- Compare Two Lists in Python: Common, Only in First, Only in Second easy
- Convert a List of Integers to a Comma-Separated String in Python easy
- Enumerate a Python List with a Custom Start Index easy
- Extract Data by Type from a List in Python: Numbers and Strings easy
- Find All Occurrences of an Item in a Python List easy
Keep learning
Related tutorials and quizzes for this topic.