Leetcode solution using Python

Posted by Lianz on July 2, 2023

题目顺序来源https://neetcode.io/practice

[toc]

Arrays & Hashing

Problem 217. Contains Duplicate

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:

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

Example 2:

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

Example 3:

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

Idea + Solution

1. hashset

1
2
3
4
5
6
7
8
9
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        hashset = set()

        for n in nums:
            if n in hashset:
                return True
            hashset.add(n)
        return False

2. set()

1
2
3
4
5
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        if len(nums) != len(set(nums)):
            return True
        return False