Contains Duplicate

Table of Contents

Problem: Contains Duplicate

Thoughts

Fairly simple question.

Given a list of values determine if any value exists at least twice.

Solution

Create a set to track elements. As you iterate over the list if any element is already in the set return true, otherwise add the element to the set. If you make it all the way through the list return false.

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        seen = set()
        for num in nums:
            if num in seen:
                return True
            seen.add(num)
        return False

Troy Fischer

2026-05-25 Mon 14:19