Contains Duplicate II - Complete Solution Guide
Contains Duplicate II is LeetCode problem 219, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java.
Problem Statement
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 . Example 1: Input: nums = [1,2,3,1], k = 3 Output: true Example 2: Input: nums = [1,0,1,1], k = 1 Output: true Example 3: Input: nums = [1,2,3,1,2,3], k = 2 Output: false Constraints: 1 <= nums.length <= 10 5 -10 9 <= nums[i] <= 10 9 0 <= k <= 10 5
Detailed Explanation
The problem asks whether an integer array contains any duplicate numbers within a specified distance `k` of each other. More formally, given an array `nums` and an integer `k`, we need to determine if there exist indices `i` and `j` such that `nums[i] == nums[j]` and `abs(i - j) <= k`. The function should return `true` if such a pair exists, and `false` otherwise. The input consists of the integer array `nums` and the integer `k`. The output is a boolean value indicating the presence or absence of the specified duplicate pair.
Solution Approach
The provided code utilizes a hash map (or HashMap in Java) to efficiently track the most recent index of each number encountered in the array. It iterates through the array. For each number, it checks if the number is already in the hash map and if the difference between the current index and the previously seen index is less than or equal to `k`. If both conditions are true, it means a duplicate within the `k` distance has been found, and the function returns `true`. Otherwise, the current number's index is updated in the hash map, and the iteration continues. If the loop completes without finding such a duplicate, the function returns `false`.
Step-by-Step Algorithm
- Step 1: Initialize an empty hash map (dictionary in Python) called `seen` to store numbers and their indices.
- Step 2: Iterate through the input array `nums` using a loop, accessing both the element and its index.
- Step 3: For each number, check if the number is a key in `seen`. If it is, check if the absolute difference between the current index and the index stored in `seen` for that number is less than or equal to `k`.
- Step 4: If the condition in Step 3 is true, return `true` because a duplicate within the specified distance is found.
- Step 5: If the condition in Step 3 is false, update the `seen` map by storing the current index associated with that number.
- Step 6: After iterating through all numbers, return `false` if no such duplicate pair was found.
Key Insights
- Insight 1: Using a hash map (dictionary in Python) to store the numbers and their most recent indices allows for efficient lookup of duplicates within the `k` distance constraint.
- Insight 2: A sliding window approach is implicitly used. While not explicitly defined by two pointers, the `i - seen[num] <= k` condition implicitly checks the elements within a window of size `k` around each element.
- Insight 3: The solution avoids unnecessary comparisons by only checking for duplicates if a number is already present in the hash map. This significantly improves efficiency compared to a brute-force approach.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Sliding Window.
Companies
Asked at: Airbnb, Arista Networks, Goldman Sachs, Google, Netflix, Palantir Technologies.