Contains Duplicate
Contains Duplicate is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Previously Seen / Existence Check subpattern.
- Blind 75 · #1 of 75 · Arrays & Hashing
- NeetCode 150 · #1 of 150 · Arrays & Hashing
- NeetCode 250 · #2 of 250 · Arrays & Hashing
Problem statement
Given nums, determine whether any integer occurs at least twice in the array. Two occurrences count as a duplicate even if they are at different positions.
Return true if nums contains a repeated value. Return false if every value appears exactly once.
Example 1:
Input: nums = [8,3,5,8] Output: true Explanation: The value 8 appears at two positions, so the array contains a duplicate.
Example 2:
Input: nums = [-4,12,0,7] Output: false Explanation: All four values are different, so no duplicate exists.
Example 3:
Input: nums = [6,6] Output: true Explanation: Both elements have the same value, which is enough to make the result true.
Constraints:
- 1 <= nums.length <= 10^5 - -10^9 <= nums[i] <= 10^9 - nums contains only integers.
Topics and companies
Topics: Array, Hash Table.
How to practise Contains Duplicate on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) expected time and O(n) space.
Open Contains Duplicate in the code editor, or read the Contains Duplicate editorial for a worked solution with its approach and complexity analysis.
Where Contains Duplicate sits in the DSA pattern sheet
- Array/Matrix Manipulation Patterns pattern guide
- Hashing - Previously Seen / Existence Check subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Contains Duplicate
Other problems that use the same Hashing - Previously Seen / Existence Check and Array/Matrix Manipulation Patterns techniques:
- Design HashSet — Easy, same subpattern
- Longest Consecutive Sequence — Medium, same subpattern
- Valid Sudoku — Medium, same subpattern
- Add Binary — Easy, same pattern
- Add to Array-Form of Integer — Easy, same pattern
- Concatenation of Array — Easy, same pattern
Opening the problem
Progress is saved as you study.