Skip to content

[217] Contains Duplicate

https://leetcode.com/problems/contains-duplicate/description/

  • algorithms
  • Easy (49.56%)
  • Source Code: 217.contains-duplicate.py
  • Total Accepted: 321.3K
  • Total Submissions: 624.3K
  • Testcase Example: '[1,2,3,1]'

Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Example 1:

Input: [1,2,3,1] Output: true

Example 2:

Input: [1,2,3,4] Output: false

Example 3:

Input: [1,1,1,3,3,4,3,2,4,2] Output: true

python
class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        table = {}
        for n in nums:
            if n not in table: table[n] = 1
            else:
                return True

        return False

Last updated: