|
| 1 | +--- |
| 2 | +id: contains-duplicate-II |
| 3 | +title: Contains Duplicate II(LeetCode) |
| 4 | +sidebar_label: 0219-Contains Duplicate II |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Hash Table |
| 8 | + - Sliding Window |
| 9 | +description: 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. |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Statement |
| 13 | + |
| 14 | +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`. |
| 15 | + |
| 16 | +### Examples |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +```plaintext |
| 21 | +Input: nums = [1,2,3,1], k = 3 |
| 22 | +Output: true |
| 23 | +``` |
| 24 | + |
| 25 | +**Example 2:** |
| 26 | + |
| 27 | +```plaintext |
| 28 | +Input: nums = [1,0,1,1], k = 1 |
| 29 | +Output: true |
| 30 | +``` |
| 31 | + |
| 32 | +**Example 3:** |
| 33 | + |
| 34 | +```plaintext |
| 35 | +Input: nums = [1,2,3,1,2,3], k = 2 |
| 36 | +Output: false |
| 37 | +``` |
| 38 | + |
| 39 | +### Constraints |
| 40 | + |
| 41 | +- `1 <= nums.length <= 105` |
| 42 | +- `109 <= nums[i] <= 109` |
| 43 | +- `0 <= k <= 105` |
| 44 | + |
| 45 | +## Solution |
| 46 | + |
| 47 | +We can solve this problem using a HashSet or a HashMap to keep track of the elements within the sliding window of size k. Here, we discuss Java, C++, Python, and JavaScript solutions for this problem. |
| 48 | + |
| 49 | +### Approach 1: Java Solution Using HashSet |
| 50 | + |
| 51 | +#### Algorithm |
| 52 | + |
| 53 | +1. Initialize an empty HashSet. |
| 54 | +2. Traverse the array with a loop. |
| 55 | +3. For each element: |
| 56 | +* If it is already in the HashSet, return 'true'. |
| 57 | +* Add the element to the HashSet. |
| 58 | +* If the HashSet size exceeds 'k', remove the oldest element. |
| 59 | +4. If no duplicate is found within the given range, return 'false'. |
| 60 | + |
| 61 | +#### Implementation |
| 62 | + |
| 63 | +```Java |
| 64 | +class Solution { |
| 65 | + public boolean containsNearbyDuplicate(int[] nums, int k) { |
| 66 | + if (nums == null || nums.length < 2 || k == 0) return false; |
| 67 | + HashSet<Integer> hset = new HashSet<Integer>(); |
| 68 | + for (int j = 0; j < nums.length; j++) { |
| 69 | + if (!hset.add(nums[j])) return true; |
| 70 | + if (hset.size() >= k + 1) { |
| 71 | + hset.remove(nums[j - k]); |
| 72 | + } |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Complexity Analysis |
| 80 | + |
| 81 | +- **Time complexity**: O(N) |
| 82 | +- **Space complexity**: O(k) |
| 83 | + |
| 84 | +### Approach 2: C++ Solution Using Unordered Set |
| 85 | + |
| 86 | +#### Algorithm |
| 87 | + |
| 88 | +1. Initialize an unordered set. |
| 89 | +2. Traverse the array with a loop. |
| 90 | +3. For each element: |
| 91 | +* Check if it is in the set. If yes, return 'true'. |
| 92 | +* Insert the element into the set. |
| 93 | +* If the set size exceeds 'k', remove the oldest element. |
| 94 | +4. If no duplicate is found, return 'false'. |
| 95 | + |
| 96 | +#### Implementation |
| 97 | + |
| 98 | +```C++ |
| 99 | +class Solution { |
| 100 | +public: |
| 101 | + bool containsNearbyDuplicate(vector<int>& nums, int k) { |
| 102 | + unordered_set<int> hset; |
| 103 | + for (int idx = 0; idx < nums.size(); idx++) { |
| 104 | + if (hset.count(nums[idx])) return true; |
| 105 | + hset.insert(nums[idx]); |
| 106 | + if (hset.size() > k) { |
| 107 | + hset.erase(nums[idx - k]); |
| 108 | + } |
| 109 | + } |
| 110 | + return false; |
| 111 | + } |
| 112 | +}; |
| 113 | +``` |
| 114 | +
|
| 115 | +### Approach 3: Python Solution Using Dictionary |
| 116 | +
|
| 117 | +#### Algorithm |
| 118 | +
|
| 119 | +1. Initialize an empty dictionary. |
| 120 | +2. Traverse the array with a loop. |
| 121 | +3. For each element: |
| 122 | +* Check if the element exists in the dictionary and the difference between the current index and the stored index |
| 123 | + is less than or equal to 'k'. If yes, return 'true'. |
| 124 | +* Update the dictionary with the current element and its index. |
| 125 | +4. If no duplicate is found, return 'false'. |
| 126 | +
|
| 127 | +#### Implementation |
| 128 | +
|
| 129 | +```Python |
| 130 | +class Solution: |
| 131 | + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: |
| 132 | + hset = {} |
| 133 | + for idx in range(len(nums)): |
| 134 | + if nums[idx] in hset and abs(idx - hset[nums[idx]]) <= k: |
| 135 | + return True |
| 136 | + hset[nums[idx]] = idx |
| 137 | + return False |
| 138 | +``` |
| 139 | + |
| 140 | +### Complexity Analysis |
| 141 | + |
| 142 | +- **Time complexity**: O(N) |
| 143 | +- **Space complexity**: O(k) |
| 144 | + |
| 145 | +### Approach 4: JavaScript Solution Using HashMap |
| 146 | + |
| 147 | +#### Algorithm |
| 148 | + |
| 149 | +1. Initialize an empty Map. |
| 150 | +2. Traverse the array with a loop. |
| 151 | +3. For each element: |
| 152 | +* Check if it exists in the map and the difference between indices is less than or equal to 'k'. If yes, return |
| 153 | + 'true'. |
| 154 | +* Update the map with the current element and its index. |
| 155 | +4. If no duplicate is found, return 'false'. |
| 156 | + |
| 157 | +#### Implementation |
| 158 | + |
| 159 | +```Javascript |
| 160 | +var containsNearbyDuplicate = function(nums, k) { |
| 161 | + const hasmap = new Map(); |
| 162 | + for (let idx = 0; idx < nums.length; idx++) { |
| 163 | + if (hasmap.has(nums[idx]) && idx - hasmap.get(nums[idx]) <= k) { |
| 164 | + return true; |
| 165 | + } |
| 166 | + hasmap.set(nums[idx], idx); |
| 167 | + } |
| 168 | + return false; |
| 169 | +}; |
| 170 | +``` |
| 171 | + |
| 172 | +### Complexity Analysis |
| 173 | + |
| 174 | +- **Time complexity**: O(N) |
| 175 | +- **Space complexity**: O(k) |
| 176 | + |
| 177 | + |
| 178 | +### Conclusion |
| 179 | + |
| 180 | +All four solutions utilize a similar strategy of keeping track of the elements within a sliding window of size 'k'. |
| 181 | +This ensures that we can efficiently determine if any two indices have the same value and are within 'k' distance of |
| 182 | +each other. Each solution is optimal with a time complexity of O(n) and a space complexity of O(k). |
0 commit comments