-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path219.contains-duplicate-ii.js
59 lines (59 loc) · 1.04 KB
/
219.contains-duplicate-ii.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @lc app=leetcode id=219 lang=javascript
*
* [219] Contains Duplicate II
*
* https://leetcode.com/problems/contains-duplicate-ii/description/
*
* algorithms
* Easy (34.15%)
* Total Accepted: 173.6K
* Total Submissions: 508.3K
* Testcase Example: '[1,2,3,1]\n3'
*
* Given an array of integers and an integer k, find out whether there are two
* distinct indices i and j in the array such that nums[i] = nums[j] and the
* absolute difference between i and j is at most k.
*
*
* Example 1:
*
*
* Input: nums = [1,2,3,1], k = 3
* Output: true
*
*
*
* Example 2:
*
*
* Input: nums = [1,0,1,1], k = 1
* Output: true
*
*
*
* Example 3:
*
*
* Input: nums = [1,2,3,1,2,3], k = 2
* Output: false
*
*
*
*
*
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var containsNearbyDuplicate = function(nums, k) {
const lastIndex = new Map();
return nums.some((n, i) => {
if (lastIndex.has(n) && i - lastIndex.get(n) <= k)
return true;
lastIndex.set(n, i);
return false;
})
};