Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Count unique elementsE02. Remove duplicates from a listE03. Check if an array contains duplicatesE

Problem No 3

Check if an array contains duplicates

Easy

10 minute session

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.

Code editor
Loading editor…

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.

03:43 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.