Contains Duplicate

EasyArrays and hashing

Given an array of integers nums, return true if any value appears at least twice, and false if every value is different.

Examples

Example 1
Input:  nums = [3, 8, 1, 8]
Output: true

8 appears twice.

Example 2
Input:  nums = [4, 5, 6]
Output: false

All three values are different.

Constraints

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

Stuck?

Hint 1

Comparing every pair works, but it takes O(n²) time.

Hint 2

A Set remembers what you have seen. If a number is already in it, you have your answer.

Still stuck? Read the solution

JavaScript
Loading the editor

Run checks your code against the 2 examples. Submit checks all 9 tests, most of them hidden.

Ctrl + Enter runs, and adding Shift submits. On a Mac, use ⌘.

blur