Skip to content
Veltoryx Technologies
FreeProblem 1● EasyLeetCode #217 ↗3 min readUpdated 30 October 2026

Contains Duplicate

Find out whether any number appears twice by remembering every number you've already seen.

01The problem

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

InputOutputWhy
[1, 2, 3, 1]true1 appears twice
[1, 2, 3, 4]falseall different
[]falsenothing to repeat

Constraints: up to 100,000 numbers, each between −10⁹ and 10⁹.

02How to spot it

The question is literally "have I seen this before?" That's the signal for a HashSet: one pass, asking the set before adding to it.

03Pseudocode

text
seen = empty set
for each n in nums:
    if n is in seen: return true
    add n to seen
return false
  • seen holds every number to the left of where we are.
  • If the current number is already in seen, it appeared earlier: that's a duplicate.
  • If we finish without a match, all numbers were different.

04Watch it work

Visualizer · step through the algorithmnums = [1,2,3,1]
10
21
32
13

seen

empty

Start with an empty set called seen. It will remember every number we pass.

Pseudocode

  1. seen = empty set
  2. for each n in nums:
  3. if n is in seen: return true
  4. add n to seen
  5. return false

Variables

i
–
n
–
step 1 / 5

05Step-by-step dry run

  1. 1
    10
    21
    32
    13

    seen

    empty

    Start with an empty set called seen. It will remember every number we pass.

    i = –n = –

  2. 2
    10
    21
    32
    13
    i

    seen

    1

    1 isn't in seen yet, so it's new. Add it and move on.

    i = 0n = 1

  3. 3
    10
    21
    32
    13
    i

    seen

    12

    2 isn't in seen yet, so it's new. Add it and move on.

    i = 1n = 2

  4. 4
    10
    21
    32
    13
    i

    seen

    123

    3 isn't in seen yet, so it's new. Add it and move on.

    i = 2n = 3

  5. 5
    10
    21
    32
    13
    i

    seen

    123

    1 is already in seen: we met it at index 0. That's a duplicate, so return true straight away.

    i = 3n = 1

06Java solution

ContainsDuplicate.java
import java.util.HashSet;
import java.util.Set;

public class ContainsDuplicate {
    static boolean containsDuplicate(int[] nums) {
        Set<Integer> seen = new HashSet<>();
        for (int n : nums) {
            if (!seen.add(n)) return true;   // add returns false when n was already there
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(containsDuplicate(new int[] {1, 2, 3, 1}));
        System.out.println(containsDuplicate(new int[] {1, 2, 3, 4}));
        System.out.println(containsDuplicate(new int[] {}));
    }
}
Expected output
true
false
false

The tricky line: seen.add(n) both checks and adds. It returns false when n was already in the set, so one call replaces contains plus add.

07Why it works

Before looking at index i, the set holds exactly nums[0..i−1]. So "is nums[i] in the set?" is the same as "did nums[i] appear earlier?" The first time the answer is yes, we've found a duplicate.

08Time and space

  • Time: O(n). One pass; each set operation is O(1) on average.
  • Space: O(n). In the worst case (no duplicates) the set holds every number.

09Edge cases

CaseResult
Empty arrayfalse
One elementfalse
All the same [7,7,7]true at index 1
Negative numbersWork the same way

10Common mistakes

  • Two nested loops. Correct but O(n²): too slow for 100,000 numbers.
  • Adding before checking. Then every number "is in the set" and you always return true.
  • Using List.contains. A list search is O(n), so the whole thing becomes O(n²) again.

11Other approaches

ApproachTimeExtra spaceNote
Compare every pairO(n²)O(1)Too slow for big inputs
HashSetO(n)O(n)Best general answer
Sort, check neighboursO(n log n)O(1)Changes the array; good if memory is tight

12In the interview

Common follow-ups:

  • Duplicates within k positions? Keep only the last k numbers in the set (Contains Duplicate II).
  • Return the duplicate instead? Return n instead of true.

13Check your understanding

Quiz · 3 questions

0 of 3 answered

  1. 1. Why is a HashSet better than a List for "seen"?

  2. 2. What does seen.add(5) return if 5 is already in the set?

  3. 3. What is the space cost of the HashSet solution?

14Solve it yourself

Try it on LeetCode #217 without looking back at this page.