202. Contains Duplicate

Easy
Frequency Set
Array

Problem

Given an integer array nums, return true if any value appears at least twice in the array. Return false if every element is distinct.

Examples

Example 1

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

Example 2

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

Example 3

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

1 <= nums.length <= 10⁵

-10⁹ <= nums[i] <= 10⁹

Hints

?? Think about remembering numbers you have already seen.

?? A Set provides fast membership checks.

?? If a number already exists in the Set, you found a duplicate.

Expected Complexity
Time: O(n)
Space: O(n)
Follow-up

Can you solve it in O(n) expected time?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,2,3,1]
Expected
true