Introduction
The Contains Duplicate problem involves checking whether any element appears more than once in the array.
Given an array arr[] of size n, the task is to determine whether the array contains duplicate elements.
Return:
- true if any value appears at least twice
- false if all elements are distinct
This is one of the most important beginner-level Hashing problems and helps in understanding HashSet, efficient lookup operations, and duplicate detection techniques.
Example
Input: arr[] = [1, 2, 3, 4, 2]Output: true
Explanation:
Element 2 appears more than once.
Therefore, the array contains duplicates.
Input: arr[] = [5, 8, 1, 9]
Output: false
Explanation:
All elements are unique.
No duplicate element exists.
Constraints
1 <= n <= 10^5-10^9 <= arr[i] <= 10^9
Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Compare every element with all remaining elements
- If any two elements are equal:
- duplicate exists
This approach is easy to understand but repeated comparisons increase time complexity.
Steps
- Traverse the array using index i.
- Compare arr[i] with every element after it.
- If duplicate is found:
- return true
- If traversal completes:
- return false
Dry Run
Input Array: [1, 2, 3, 4, 2]Compare 1 with remaining elements:
No duplicate found
Compare 2 with remaining elements:
2 == 2
Duplicate found
Return true
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)
Explanation:
Every element is compared with remaining elements.
Space Complexity: O(1)
Explanation:No extra data structures are used.
Approach 2 : Optimized Solution (HashSet)
Explanation
Instead of repeatedly comparing elements, we can use a HashSet.
The idea is:
- Traverse the array
- Store visited elements in HashSet
- If current element already exists:
- duplicate found immediately
HashSet provides fast lookup operations.
Steps
- Create an empty HashSet.
- Traverse the array.
- For every element:
- check if element already exists
- If exists:
- return true
- Otherwise:
- insert element into HashSet
- If traversal finishes:
- return false
Dry Run
Input Array: [1, 2, 3, 4, 2]Initially:
HashSet = {}
Traverse 1:
Not present
Insert 1
HashSet = {1}
Traverse 2:
Not present
Insert 2
HashSet = {1, 2}
Traverse 3:
Insert 3
HashSet = {1, 2, 3}
Traverse 4:
Insert 4
HashSet = {1, 2, 3, 4}
Traverse 2:
2 already exists
Duplicate found
Return true
Optimized Code
Complexity Analysis
Time Complexity: O(n)
Explanation: Each element is checked once using HashSet lookup.
Space Complexity: O(n)
Explanation: HashSet stores array elements.
Edge Cases
- Array contains all unique elements
- Array contains all duplicates
- Array contains one element
- Array contains negative numbers
- Duplicate appears multiple times
Why This Problem is Important
This problem helps in understanding:
- Hashing techniques
- HashSet usage
- Fast lookup operations
- Duplicate detection
- Efficient traversal
It is one of the most important beginner-level hashing interview problems.
Real-World Applications
Duplicate detection is used in:
- Database systems
- User authentication systems
- Fraud detection
- Data cleaning pipelines
- Inventory management systems
Common Mistakes
- Forgetting to insert elements into HashSet
- Incorrect duplicate checks
- Using list instead of HashSet
- Breaking loops incorrectly
Interview Tips
Interviewers often expect:
- HashSet optimization
- O(n) solution
- Efficient lookup explanation
Always explain why HashSet provides faster duplicate detection compared to nested loops.
Related Questions
- Frequency Count
- Majority Element
- Two Sum
- First Missing Positive
- Longest Consecutive Sequence
Final Takeaway
The Contains Duplicate problem is a fundamental hashing problem that teaches efficient lookup operations and duplicate detection techniques using HashSet. Understanding this problem builds a strong foundation for advanced hashing and array interview problems.