Skip to content

Commit

Permalink
Create 0219-contains-duplicate-ii.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
drxlx committed Mar 27, 2024
1 parent beb0e1c commit 33e9ae0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions swift/0219-contains-duplicate-ii.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Question Link: https://leetcode.com/problems/contains-duplicate-ii/
*/

class Solution {
func containsNearbyDuplicate(_ nums: [Int], _ k: Int) -> Bool {
var hashset = Set<Int>()
var l = 0
for r in 0..<nums.count {
if r - l > k {
hashset.remove(nums[l])
l += 1
}
if hashset.contains(nums[r]) {
return true
}
hashset.insert(nums[r])
}
return false
}
}

0 comments on commit 33e9ae0

Please sign in to comment.