Skip to main content

Contains Duplicate

Easy
Data Structures and Algorithms
Editorial
Topics:
Array
Hash Table

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.

Need help or want more practice?

Practise in an interviewGet AI Coach help