Contains Duplicate

Return true if any value appears at least twice in the array, and false if every element is distinct.

A way to picture it

You're a stamp collector sorting a shoebox. Each stamp gets checked against your album before it goes in. The moment one is already there, you've found a duplicate.

How to approach it

A HashSet remembers everything you've seen with O(1) checks. The first repeat ends the search.

Example

Input: {"nums":[1,2,3,1]}. Output: true.

Complexity: O(n) time and O(n) space. Each number is checked and stored once.

This page includes an animated walkthrough, Java and Python solutions, and a question to check your understanding.