Contains Duplicate

Problem: Contains Duplicate

The solution is quite simple. We just use a hash table to store visited element and check the visiting element is duplicate or not. Since the search time of hash table is O(1), the running time is only O(1).

Code in Python:

from collections import defaultdict

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        history = defaultdict(bool)
        for n in nums:
            if not history[n]:
                history[n] = True
            else:
                return True
        return False

results matching ""

    No results matching ""