219. Contains Duplicate II
Approach 1: Brute Force
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
for i in range(len(nums)):
for j in range(len(nums)):
if i != j and abs(i-j) <= k and nums[i] == nums[j]:
return True
return FalseApproach 2: Sliding Window
Approach 3: Dictionary of last seen positions
Last updated