One of the first array problems you’ll encounter on LeetCode is 217. Contains Duplicate. Although the problem is straightforward, it introduces an important concept: trading time for space.

In this post, I’ll walk through four different Python solutions, explain how they work, and compare their time and space complexities.


Problem

Given an integer array nums, return True if any value appears more than once in the array. Otherwise, return False.

Example 1

Input: nums = [1, 2, 3, 3]
Output: True

Example 2

Input: nums = [1, 2, 3, 4]
Output: False

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Solution 1: Brute Force

The simplest approach is to compare every element with every other element.

from typing import List

class Solution:
    def hasDuplicate(self, nums: List[int]) -> bool:
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[i] == nums[j]:
                    return True
        return False

How it works

  • Pick one element.
  • Compare it with every element after it.
  • If a match is found, return True.
  • If all comparisons finish without finding a duplicate, return False.

Complexity

  • Time: O(n²)
  • Space: O(1)

Pros

  • Easy to understand.
  • Doesn’t require extra memory.

Cons

  • Extremely slow for large arrays.
  • Not practical for the maximum constraint (10^5 elements).

Solution 2: Sort First

Instead of comparing every pair, sort the array first.

class Solution:
    def hasDuplicate(self, nums: List[int]) -> bool:
        nums.sort()

        for i in range(len(nums) - 1):
            if nums[i] == nums[i + 1]:
                return True

        return False

Why this works

Sorting places identical values next to each other.

For example:

Before sorting:
[4, 1, 3, 2, 1]

After sorting:
[1, 1, 2, 3, 4]

Now we only need to compare adjacent elements.

Complexity

  • Time: O(n log n)
  • Space: Depends on Python’s sorting implementation (typically O(log n) auxiliary stack space).

Pros

  • Faster than brute force.
  • Doesn’t require a hash set.

Cons

  • Modifies the original array.
  • Still slower than the optimal solution.

Solution 3: Hash Set

This is the most common interview solution.

class Solution:
    def hasDuplicate(self, nums: List[int]) -> bool:
        seen = set()

        for num in nums:
            if num in seen:
                return True
            seen.add(num)

        return False

How it works

A set only stores unique values.

As we iterate through the array:

  • If the current number is already in the set, we’ve found a duplicate.
  • Otherwise, add it to the set and continue.

Example:

nums = [1, 2, 3, 2]

seen = {}

1 → add
2 → add
3 → add
2 → already exists ✅

Complexity

  • Time: O(n)
  • Space: O(n)

Pros

  • Optimal time complexity.
  • Easy to understand.
  • Preferred in coding interviews.

Cons

  • Uses extra memory.

Solution 4: Python One-Liner

Python makes this problem even simpler.

class Solution:
    def hasDuplicate(self, nums: List[int]) -> bool:
        return len(set(nums)) < len(nums)

Why it works

A set automatically removes duplicates.

For example:

nums = [1, 2, 2, 3]

len(nums) = 4
len(set(nums)) = 3

Since the lengths are different, duplicates must exist.

Complexity

  • Time: O(n)
  • Space: O(n)

Pros

  • Very concise.
  • Easy to read.
  • Great for Python developers.

Cons

  • Less explicit than the previous solution.
  • In interviews, explaining the hash set approach often demonstrates your understanding better than jumping straight to the one-liner.

Complexity Comparison

Solution

Time

Space

Brute Force

O(n²)

O(1)

Sort + Compare

O(n log n)

O(log n) auxiliary

Hash Set

O(n)

O(n)

Set One-Liner

O(n)

O(n)


Key Takeaways

This problem is a great introduction to hash-based data structures.

  • Start with the brute-force solution to understand the problem.
  • Improve it by sorting and checking adjacent elements.
  • Reach the optimal solution using a hash set.
  • In Python, you can even solve it in a single line with set().

Understanding why each solution improves on the previous one is more valuable than simply memorizing the final answer. In interviews, demonstrating this progression shows strong problem-solving skills and your ability to optimize algorithms.