sliding window is simple we take to iterators and iterate them conditionally there are two type of sliding windows
1> fixed
2> dynamic
FIXED SLIDING WINDOW
function findDuplicates(nums, k){
let mySet = new Set()
let i=0;
for(i; i <= Math.min(k,nums.length-1) ; i++){
if(mySet.has(nums[i])){
return true
}else{
mySet.add(nums[i])
console.log("mySet", mySet)
}
}
i=1;
j=k+1
while(j<nums.length){
mySet.delete(nums[i-1])
if(mySet.has(nums[j])){
return true
}
else{
mySet.add(nums[j])
i++
j++
}
}
return false
}
tricky optimal method
function containNearbyDuplicate(nums, k){
const setArray = new Map()
let isFind = false
for (let i = 0; i < nums.length; i++) {
if (setArray.has(nums[i])) {
if (Math.abs(i - setArray.get(nums[i])) <= k) {
isFind = true
}
}
setArray.set(nums[i], i)
}
return isFind
}