You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[Test Frog Position After T Seconds](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/graphs/frog_position_after_t_seconds/test_frog_position_after_t_seconds.py)
83
85
* Greedy
84
86
* Min Arrows
85
87
*[Test Find Min Arrows](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/min_arrows/test_find_min_arrows.py)
*[Diff Elements In List](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/array_diff/diff_elements_in_list.py)
197
+
* Contains Duplicates
198
+
*[Test Contains Nearby Almost Duplicate](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/contains_duplicates/test_contains_nearby_almost_duplicate.py)
*[Test Binary Search Tree Search](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/search_tree/test_binary_search_tree_search.py)
*[Test Binary Tree Deserialize](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree_deserialize.py)
299
+
*[Test Binary Tree Min Camera Cover](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree_min_camera_cover.py)
293
300
*[Test Binary Tree Serialize](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree_serialize.py)
294
301
*[Test Binary Tree Visible Nodes](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree_visible_nodes.py)
*[Test Contains Nearby Almost Duplicate](https://github.com/BrianLusina/PythonSnips/blob/master/tests/datastructures/arrays/test_contains_nearby_almost_duplicate.py)
Copy file name to clipboardExpand all lines: datastructures/arrays/contains_duplicates/README.md
+57-1Lines changed: 57 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,18 @@ Example 3:
17
17
Input: nums = [1,1,1,3,3,4,3,2,4,2]
18
18
Output: true
19
19
20
+
---
21
+
20
22
## Contains Duplicates II
21
23
22
-
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.
24
+
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such
25
+
that nums[i] == nums[j] and abs(i - j) <= k.
26
+
27
+
Constraints:
28
+
29
+
- 1 <= nums.length <= 10^3
30
+
- -10^3 <= nums[i] <= 10^3
31
+
- 0 <= k <= 10^4
23
32
24
33
Example 1:
25
34
@@ -34,6 +43,53 @@ Example 3:
34
43
Input: nums = [1,2,3,1,2,3], k = 2
35
44
Output: false
36
45
46
+
### Solution
47
+
48
+
The core intuition of solving this problem is maintaining a sliding window of size k to track elements within a limited
49
+
range using a set. As we iterate through the array, we check if the current element already exists in the set,
50
+
indicating a duplicate within the range. If it exists, we return TRUE. Otherwise, the element is added to the set.
51
+
If the set size exceeds k, we remove the oldest element to ensure that the set only contains elements within the valid
52
+
range at any time.
53
+
54
+
Using the above intuition, the solution can be implemented as follows:
55
+
56
+
1. Create a set, `seen`, to track elements within the sliding window of size `k`.
57
+
2. Loop through each index `i` of the array `nums`.
58
+
- If the current element, `nums[i]`, already exists in the set, a duplicate exists within a range of `k` indices.
59
+
Therefore, we return TRUE.
60
+
- Add the current element to the set.
61
+
- If the set’s size exceeds `k`, remove the oldest element in the window (`nums[i - k]`) to maintain the window’s size.
62
+
This ensures only elements within the range k are tracked.
63
+
64
+
3. If the loop completes without finding duplicates, we return FALSE.
65
+
66
+
Let’s look at the following illustration to get a better understanding of the solution:
0 commit comments