Lesson guide
What this Python exercise practices
Check if an array contains duplicates is a beginner practice lesson that focuses on dsa, problem patterns, edge cases. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python functions
- Loops
- Lists
- Basic edge cases
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Related public exercises
Summary
Return True if any value appears more than once in the array, otherwise False.
Problem statement
Given a list arr, implement has_duplicates(arr) that returns True if there is at least one element that appears more than once, otherwise returns False. Use hashing (a set) to check efficiently: if the number of unique elements is less than the list length, duplicates exist.
Task
Implement has_duplicates(arr) to detect whether any duplicate exists using set size comparison for O(n) average time.
Examples
Duplicate present
Input
has_duplicates([1, 2, 3, 2])
Output
True
Explanation
Element 2 appears twice, so the function returns True.
Input format
A list of hashable elements (e.g., integers), e.g., [1, 2, 3, 2]
Output format
A boolean: True if duplicates exist, False otherwise.
Constraints
0 <= len(arr) <= 10^5. Elements are hashable. Aim for O(n) average time and O(n) extra space.
Samples
Sample input 0
[1, 2, 3]
Sample output 0
False
Explanation 0
All elements are unique.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.