Skip to content

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.

Example 1:

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

Solve

We can use hash map to make our cache memory of found number

  • Loop through all number If we found a collision then return True,
  • Return False
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        cache = set()
        for n in nums:
            if n in cache:
                return True
            cache.add(n)

        return False

Last update : October 13, 2023
Created : August 16, 2023