Skip to main content

Contains Duplicate

EasyArraysHash set353 tests

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

Example 1

text

Example 2

text

Sorting first works and costs O(n log n). There is an O(n) approach that trades memory for time.

Examples

Example 1

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

The value 1 appears at index 0 and index 3.

Example 2

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

Every element is distinct.

Example 3

Input:
[1,1,1,3,3,4,3,2,4,2]
Output:
true

Several values repeat; one is enough.

Constraints

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

Run checks your code against the 3 examples. Submit checks it against all 353 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.