217. Contains Duplicate
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
freq = {}
result = False
for num in nums:
if num not in freq:
freq[num] = 1
else:
result = True
break
return result
219. Contains Duplicate II
class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
num_ind={}
n=len(nums)
for i in range(len(nums)):
if nums[i] in num_ind and abs(i - num_ind[nums[i]]) <= k:
return True
num_ind[nums[i]] = i
return False