GitHub
Easy

Contains Duplicate

Given an integer array, determine if any value appears at least twice in the array.

Write a function that returns true if any value appears at least twice in the array, and false if every element is distinct.

How to Solve

Use a Set to track seen numbers. As you iterate through the array, if a number is already in the set, return true. Otherwise, add it to the set. If you finish iterating without finding duplicates, return false.

Click to reveal

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