leetcode | January 20, 2022
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.
Memory: 26 MB
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
if len(set(nums)) != len(nums):
return True
return False
Memory: 25.9 MB
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(set(nums)) != len(nums)
len()
: O(1) (including both set and list length)