219. Contains Duplicate II - Explanation
Description
You are given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k, otherwise return false.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: trueExample 2:
Input: nums = [2,1,2], k = 1
Output: falseConstraints:
1 <= nums.length <= 100,000-1,000,000,000 <= nums[i] <= 1,000,000,0000 <= k <= 100,000
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Hash Map / Hash Set - Used for O(1) lookups to track element indices or check for duplicates within a window
- Sliding Window Technique - Maintaining a fixed-size window of elements as you iterate through the array
1. Brute Force
Intuition
The simplest approach is to check every pair of elements. For each element, we look at all elements within distance k and check if any of them are equal. This guarantees finding a duplicate if one exists within the required distance.
Algorithm
- Iterate through each index
Lfrom0ton-1. - For each
L, iterate through indexRfromL+1tomin(n-1, L+k). - If
nums[L] == nums[R], returntrue. - If no such pair is found, return
false.
Time & Space Complexity
- Time complexity:
- Space complexity:
Where is the size of the array and is the maximum distance between two equal numbers.
2. Hash Map
Intuition
Instead of checking all pairs, we can store the most recent index of each value in a hash map. When we encounter a value, we check if it appeared before and if the distance to its last occurrence is within k. This gives us O(1) lookup time for duplicates.
Algorithm
- Create a hash map to store each value's most recent index.
- Iterate through the array with index
i. - If
nums[i]exists in the map andi - map[nums[i]] <= k, returntrue. - Update
map[nums[i]] = i(store the current index). - If no valid pair is found, return
false.
Time & Space Complexity
- Time complexity:
- Space complexity:
Where is the size of the array and is the maximum distance between two equal numbers.
3. Hash Set
Intuition
We only need to check for duplicates within a sliding window of size k. Using a hash set, we maintain exactly the elements in the current window. If a new element already exists in the set, we found a duplicate within distance k. We slide the window by removing the leftmost element when the window exceeds size k.
Algorithm
- Create an empty hash set to represent the sliding window.
- Use two pointers
LandR, withRiterating through the array. - If the window size
R - Lexceedsk, removenums[L]from the set and incrementL. - If
nums[R]is already in the set, returntrue. - Add
nums[R]to the set. - If no duplicate is found, return
false.
Time & Space Complexity
- Time complexity:
- Space complexity:
Where is the size of the array and is the maximum distance between two equal numbers.
Common Pitfalls
Off-by-One Error in Distance Check
The condition requires the distance to be "at most k", meaning abs(i - j) <= k, not < k. Using strict inequality will miss valid pairs that are exactly k positions apart.
# Wrong: Using strict inequality
if nums[i] in mp and i - mp[nums[i]] < k: # Misses distance == k
# Correct: Use <= for "at most k"
if nums[i] in mp and i - mp[nums[i]] <= k:Storing All Indices Instead of Most Recent
The hash map only needs to store the most recent index for each value. Storing all indices wastes memory and complicates the logic. Since we iterate left to right, we only care about the closest previous occurrence.
Sign in to join the discussion