Contains Duplicate

2Easy

Contains Duplicate

Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.


Example 1

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

Explanation: The element 1 appears at index 0 and index 3.

Example 2

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

Explanation: All elements are distinct.

Example 3

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

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Hints

๐Ÿ’ก Hint 1

Think about what data structure lets you check "have I seen this before?" in O(1) time.

๐Ÿ’ก Hint 2

A HashSet stores unique values. If you try to add a value that's already there, you've found a duplicate.

๐Ÿ’ก Hint 3

There's a one-liner approach: compare the length of the array vs the length of the set of the array.

JR

Jnaneshwara Reddy Satti

Builder ยท ClearCode

Hi โ€” I'm a 21yo CS grad from India. I built ClearCode while recovering from a surgery, because I genuinely believe DSA should be visual, free, and accessible to everyone. No VC funding. No team. Just me, my laptop, and a lot of chai. โ˜•

If ClearCode helped you understand even one concept โ€” consider supporting it. Every rupee keeps the servers running and new videos coming.

Support via Razorpay
Support ClearCode โ˜•