Contains Duplicates II
Last updated
Was this helpful?
Last updated
Was this helpful?
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Using a dictionary, linearly go through the array and check if the number already exists in the dictionary. If it does, the the abosulte difference between their indexes HOWEVER if the difference is more than k update the index of that number in the dictionary.
Time: O(n)
Space: O(n)