Contains duplicate II

October 21, 2022

array-and-hashmap

Problem URL: Contains duplicate II

We will use a hashmap to store the last index of each number. For each number, we will check if the difference between the current index and the last index is less than or equal to k. If it is, then we will return true. Otherwise, we will update the last index of the number.

class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        lookup = {}
        for i, num in enumerate(nums):
            if num in lookup and i - lookup[num] <= k:
                return True
            lookup[num] = i
        return False

Time Complexity: O(n)
Space Complexity: O(n)