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
Contains Duplicate: Problem OverviewInput: [1, 2, 3, 1] → Check for duplicates → Output: true1231index 0index 1index 2index 3Same value: 1Duplicate Found!Output: true
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
Asked in
Google 25 Amazon 30 Microsoft 20 Apple 15
125.0K Views
High Frequency
~15 min Avg. Time
3.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen