SDE & MLE Prep

217. Contains Duplicate

Problem

Return true if some value appears at least twice in the integer array nums, and false if all elements are distinct.

Example: [1,2,3,1] → true; [1,2,3,4] → false

Constraints: n ≤ 10⁵.

Insert into a set; if an element is already there, it's a duplicate.

Complexity: O(n) time, O(n) space (sorting gives O(n log n) / O(1)).

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