Contains Duplicate

EasyArrayHash Table

Description

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.

Examples

Input:nums = [1,2,3,1]
Output:true
Explanation:

Using a hash set to track seen values, This finds that 1 appears at index 0 and again at index 3, confirming a duplicate exists.

Input:nums = [1,2,3,4]
Output:false
Explanation:

After checking all elements against a hash set, no value is encountered twice. Each of 1, 2, 3, and 4 appears exactly once, so no duplicate exists.

Input:nums = [1,1,1,3,3,4,3,2,4,2]
Output:true
Explanation:

Multiple values repeat: 1 appears twice, 3 appears three times, 4 appears twice, and 2 appears twice. The algorithm returns true as soon as the first duplicate is detected.

Constraints

  • 1 ≤ nums.length ≤ 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!