Contains Duplicate II

Problem Summary

(Write in your own words, not copied from LeetCode. This forces comprehension.)

Key Observations

(Patterns, constraints, or hints in the problem statement.)

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        numToLastIndex = {}
        for currentIndex, num in enumerate(nums):
            if num in numToLastIndex: # we know when was it last found, do calc
                indexOfLastNum = numToLastIndex[num]
                if abs(currentIndex - indexOfLastNum) <= k:
                    return True
            numToLastIndex[num] = currentIndex
        return False

Common Mistakes / Things I Got Stuck On