easy

Contains Duplicate

Answer whether any value repeats in an array by keeping every value seen so far in a hash set and stopping at the first repeat.

1. Define the problem

Contains Duplicate

Given an integer array nums, return true if any value appears at least twice , and false if every element is different. Keep a hash set of the numbers you have already walked past. For each new number, first ask the set whether it is already in there. If it is, you have found a repeat and can stop immediately. If it is not, put it in the set and move on. Asking a set whether it holds a value costs about the same tiny amount of time however many values it holds, which is why this beats comparing every pair.

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ numsi ≤ 109

Example

Inputnums = [1, 2, 3, 1]
Outputtrue

Explanation The value 1 appears at index 0 and again at index 3, so there is a duplicate.

2. Know the words first

In plain terms

Hash set
A bag of values that answers one question very fast: "is this value already in here?" It never stores the same value twice.
Duplicate
The same value appearing more than once in the array, no matter how far apart the two copies are.
3. Visualize the solution

Each cell is a slot in the hash set, filled as numbers are added

Each cell is a slot in the hash set, filled as numbers are added
Statusinit

The hash set starts empty, so every slot shows a dash.

What happens in this step

nums = [1, 2, 3, 1]
seen = empty

Nothing has been walked past yet.
The four slots stand for the values the set may end up holding.
Step 1 of 6

Steps to visualize

  1. The cells below are the hash set. A dash means that slot is still empty.
  2. Walk the array one number at a time.
  3. Before storing a number, ask the set whether it is already there.
  4. If the set says yes, the answer is true and you can stop early.
  5. If the set says no, add the number and carry on.
4. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Each cell is a slot in the hash set, filled as numbers are added
Statusinit

The hash set starts empty, so every slot shows a dash.

What happens in this step

nums = [1, 2, 3, 1]
seen = empty

Nothing has been walked past yet.
The four slots stand for the values the set may end up holding.
Step 1 of 6
5. Solution

Solution

solution.tsTypeScript
function containsDuplicate(nums) {
  const seen = new Set();

  for (const num of nums) {
    if (seen.has(num)) {
      return true;
    }

    seen.add(num);
  }

  return false;
}
Time
O(n)
Space
O(n)
6. Test cases

Test cases

InputExpectedCovers
nums = [1, 2, 3, 1]trueexample from the description
nums = [1, 2, 3, 4]falseevery value is unique
nums = [7]falsesmallest input, one element cannot repeat
nums = [-1, -1]truenegative numbers repeating
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]truelonger array where the repeat is only found at the end
nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]trueseveral different values repeat