Skip to content

Intuition

Very classy way of using hash to find duplicate.

Approach

Complexity

  • Time complexity: \(O(n)\) assuming good implemention of hash
  • Space complexity: \(O(1)\)

Code

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        #
        hashtable = {}

        for i in range(len(nums)):
            # check if current element
            if nums[i] in hashtable.keys():
                return True

            # assign key-value pair as value-index 
            hashtable[nums[i]] = i

        return False