forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainsDuplicateII.kt
39 lines (35 loc) · 1.42 KB
/
ContainsDuplicateII.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* 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`.
*
* [Source](https://leetcode.com/problems/contains-duplicate-ii/)
*/
@UseCommentAsDocumentation
private fun containsNearbyDuplicate(nums: IntArray, k: Int): Boolean {
// How about using hash set instead of window
val hashSet = LinkedHashSet<Int>()
for (i in 0..nums.lastIndex) {
if (i > k) { // now start removing items
// remove that element which is out of the window of width [k]
val elemToBeRemoved = nums[i - (k + 1)]
hashSet.remove(elemToBeRemoved)
}
val current = nums[i]
// if the current window contains that element, then duplicate present
if (hashSet.contains(current)) {
return true
}
hashSet.add(current)
}
return false
}
fun main() {
containsNearbyDuplicate(nums = intArrayOf(1, 2, 3, 9, 5, 6, 2), k = 3) shouldBe false
containsNearbyDuplicate(nums = intArrayOf(1, 2, 3, 1), k = 3) shouldBe true
containsNearbyDuplicate(nums = intArrayOf(1, 0, 1, 1), k = 1) shouldBe true
containsNearbyDuplicate(nums = intArrayOf(1, 2, 3, 1, 2, 3), k = 2) shouldBe false
containsNearbyDuplicate(nums = intArrayOf(1, 2, 3, 4, 1), k = 3) shouldBe false
}