โ† Blogs
May 9, 2026โ€ข

Leetcode 217 Contains Duplicate

ullas kunder
Ullas Kunder

Designer & Developer

Table of Contents ยท 15 sections
  1. LeetCode 217: Contains Duplicate
  2. ๐Ÿงฉ The Problem
  3. Examples
  4. Constraints
  5. ๐Ÿง  How to Think About This
  6. 1. Brute Force (Check everything)
  7. 2. Sorting (Order them up)
  8. 3. Hash Set (The Memory Trick)
  9. ๐Ÿ” Visualizing the Hash Set Approach
  10. ๐Ÿ’ป Solution (Hash Set)
  11. Results
  12. โฑ๏ธ Complexity Analysis
  13. โš ๏ธ Common Mistakes
  14. ๐Ÿง  How to Recognize This Pattern
  15. ๐ŸŽฏ Interview Tips

LeetCode 217: Contains Duplicate

Have you ever looked at a list of names and wondered if someone's name appears twice? Maybe you're checking a guest list for a party or a list of IDs in a database. That's exactly what this problem asks us to do with an array of integers. ๐Ÿง

๐Ÿงฉ The Problem

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Examples

Example 1:

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

Explanation: The element 1 occurs at indices 0 and 3. โœ…

Example 2:

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

Explanation: All elements are distinct. โŒ

Example 3:

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

Constraints

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

๐Ÿง  How to Think About This

We need to check if there are any repeating numbers. There are three common ways to solve this, each with its own trade-offs:

1. Brute Force (Check everything)

Compare every pair of numbers.

  • Logic: For each number, look at every other number.
  • Time: O(n2)O(n^2)O(n2) โ€” Too slow for large arrays. ๐Ÿข
  • Space: O(1)O(1)O(1).

2. Sorting (Order them up)

If we sort the array, any duplicates will be right next to each other.

  • Logic: Sort [1, 3, 2, 1] โ†’ [1, 1, 2, 3]. Now check if nums[i] == nums[i+1].
  • Time: O(nlogโกn)O(n \log n)O(nlogn) โ€” Pretty good! ๐ŸŽ๏ธ
  • Space: O(1)O(1)O(1) or O(n)O(n)O(n) depending on the sorting algorithm.

3. Hash Set (The Memory Trick)

Use a "notebook" (Set) to remember numbers we've seen.

  • Logic: As we walk through the array, check if the current number is in our set. If it is, we found a duplicate! If not, add it to the set and move on.
  • Time: O(n)O(n)O(n) โ€” Optimal! ๐Ÿš€
  • Space: O(n)O(n)O(n) โ€” We need extra memory for the set.

๐Ÿ” Visualizing the Hash Set Approach

nums = [1, 2, 3, 1]
seen = set()

1. Current: 1 | Is 1 in seen? No. | Add 1 to seen โ†’ {1}
2. Current: 2 | Is 2 in seen? No. | Add 2 to seen โ†’ {1, 2}
3. Current: 3 | Is 3 in seen? No. | Add 3 to seen โ†’ {1, 2, 3}
4. Current: 1 | Is 1 in seen? YES! | Return True ๐ŸŽ‰

๐Ÿ’ป Solution (Hash Set)

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        # We use a set for O(1) average time complexity lookups
        seen = set()
        
        for num in nums:
            # If we've seen this number before, we found a duplicate
            if num in seen:
                return True
            # Otherwise, record that we've seen it
            seen.add(num)
            
        # If we finish the loop, all numbers are unique
        return False

Results

Runtime: 17 ms    โ€” Beats 66.43%
Memory: 25.73 MB  โ€” Beats 77.93%

โฑ๏ธ Complexity Analysis

Approach Time Complexity Space Complexity
Brute Force O(n2)O(n^2)O(n2) O(1)O(1)O(1)
Sorting O(nlogโกn)O(n \log n)O(nlogn) O(1)O(1)O(1) or O(n)O(n)O(n)
Hash Set O(n)O(n)O(n) O(n)O(n)O(n)

The Hash Set approach is generally preferred in interviews because it gives us the best time complexity, and O(n)O(n)O(n) space is usually acceptable.

โš ๏ธ Common Mistakes

  1. Using a List instead of a Set โ€” If you do seen = [] and if num in seen:, the lookup becomes O(n)O(n)O(n), making your total time complexity O(n2)O(n^2)O(n2). Always use a Set for O(1)O(1)O(1) lookups!
  2. Returning too early โ€” Make sure you only return False after the entire loop finishes.
  3. Overcomplicating with a Hash Map โ€” You don't need to count the frequencies; just knowing if it exists once is enough. A Set is cleaner than a Map/Dictionary here.

๐Ÿง  How to Recognize This Pattern

When you see these keywords in an interview:

  • "at least twice"
  • "distinct"
  • "duplicate"
  • "unique"

Think:

HASH SET or SORTING

๐ŸŽฏ Interview Tips

  1. Mention the Trade-offs โ€” "I can solve this in O(nlogโกn)O(n \log n)O(nlogn) time with O(1)O(1)O(1) space using sorting, or O(n)O(n)O(n) time with O(n)O(n)O(n) space using a hash set. Which would you prefer?"
  2. One-liner Trick โ€” In Python, you can also solve this in one line: return len(nums) != len(set(nums)). However, the loop approach is often better because it can early return as soon as it finds the first duplicate!
  3. Constraints Matter โ€” If the array is very large and memory is tight, sorting might be better than a hash set.
  4. Explain the Set โ€” Briefly explain why a set is fast (O(1)O(1)O(1) average time for searching).

โ† Previous

leetcode 207 course schedule

Next โ†’

leetcode 297 serialize and deserialize binary tree