Skip to content

Commit

Permalink
Create 0219-contains-duplicate-ii.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Feb 24, 2023
1 parent e4c39e0 commit bcdf4ad
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions kotlin/0219-contains-duplicate-ii.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
fun containsNearbyDuplicate(nums: IntArray, k: Int): Boolean {

val hs = HashSet<Int>()
var left = 0
var right = 0

while (right < nums.size) {

if (right - left > k) {
hs.remove(nums[left])
left++
}

if (nums[right] in hs) return true

hs.add(nums[right])
right++
}

return false
}
}

0 comments on commit bcdf4ad

Please sign in to comment.