Contains Duplicate - Problem
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
This is a fundamental problem that tests your understanding of data structures and algorithms for detecting duplicates efficiently.
Input & Output
Example 1 — Contains Duplicate
$
Input:
nums = [1,2,3,1]
›
Output:
true
💡 Note:
The value 1 appears at both index 0 and index 3, so there is a duplicate.
Example 2 — All Distinct
$
Input:
nums = [1,2,3,4]
›
Output:
false
💡 Note:
All elements are distinct, no duplicates found.
Example 3 — Multiple Duplicates
$
Input:
nums = [1,1,1,3,3,4,3,2,4,2]
›
Output:
true
💡 Note:
Several values appear multiple times (1, 2, 3, and 4), so there are duplicates.
Constraints
- 1 ≤ nums.length ≤ 105
- -109 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of integers [1,2,3,1]
2
Process
Check for duplicate values using hash set
3
Output
Return true if duplicate found, false otherwise
Key Takeaway
🎯 Key Insight: Use a hash set to detect duplicates in O(n) time with constant lookup
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code