Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.
**Input:** nums = [1, 2, 3, 1]
**Output:** true
Explanation: The element 1 appears at index 0 and index 3.
**Input:** nums = [1, 2, 3, 4]
**Output:** false
Explanation: All elements are distinct.
**Input:** nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]
**Output:** true
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9Think about what data structure lets you check "have I seen this before?" in O(1) time.
A HashSet stores unique values. If you try to add a value that's already there, you've found a duplicate.
There's a one-liner approach: compare the length of the array vs the length of the set of the array.
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.