-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
40 lines (35 loc) · 822 Bytes
/
res.js
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
40
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var containsNearbyDuplicate = function(nums, k) {
const dupList = [];
if (k === 0) return false;
for (let index = 0; index < nums.length; index++) {
const element = nums[index];
if (dupList.includes(element)) {
return true;
} else if (dupList.length === k) {
dupList.shift();
}
dupList.push(element);
}
return false;
};
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var containsNearbyDuplicate_2 = function(nums, k) {
const visited = {};
for(let i = 0; i < nums.length; i++) {
const num = nums[i];
if (visited[num] !== undefined && i - visited[num] <= k) {
return true;
}
visited[num] = i;
}
return false
};