This solution is designed to address the problem of determining whether there are two distinct indices i
and j
in an
array nums
such that nums[i] == nums[j]
and the absolute difference between i
and j
is less than or equal to a
given number k
.
The core idea is to keep track of each number's latest occurrence index in the array. If the same number is encountered
again, the solution checks whether the distance between the current and the previous occurrences is within the specified
range k
.
-
HashMap for Storage: A
HashMap<Integer, Integer>
nameduniqueNumbers
is used. Here, the key is the number from the array, and the value is the index of its last occurrence. -
Iterating through the Array:
- The array
nums
is iterated using a for-loop. - For each number in the array (
nums[i]
):- Check for Duplicate within Range:
- If
uniqueNumbers
already containsnums[i]
, it implies that this number has been seen before. - The solution then checks if the absolute difference between the current index
i
and the stored indexuniqueNumbers.get(nums[i])
is less than or equal to `k. - If it is,
true
is returned, indicating that a duplicate within the specified range exists.
- If
- Check for Duplicate within Range:
- The array
-
Updating the HashMap:
- Whether a duplicate is found or not, the current index
i
is stored in theuniqueNumbers
map fornums[i]
. - This update is crucial as it ensures that the map always holds the latest index for each number.
- Whether a duplicate is found or not, the current index
- Time Complexity: O(N), where N is the length of the array
nums
.- The algorithm iterates through the array once. The operation of checking and updating the HashMap takes constant time O(1) for each element, making the overall time complexity linear.
- Space Complexity: O(N), in the worst case.
- In the worst-case scenario, where all elements are unique, the HashMap uniqueNumbers might end up storing an entry for every element in the array. Hence, the space complexity is O(N).
Overall, the solution efficiently uses a HashMap to keep track of indices of elements and checks for duplicates within the specified range in a single pass through the array.