8000 contains-duplicate-ii · malipramod/leetcode-js@2e2336d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e2336d

Browse files
committed
contains-duplicate-ii
1 parent c38f5f0 commit 2e2336d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {boolean}
5+
*/
6+
var containsNearbyDuplicate = function (nums, k) {
7+
let i = 0;
8+
let myMap = {};
9+
while (i < nums.length) {
10+
if (myMap[nums[i]] >= 0 && i - myMap[nums[i]] <= k)
11+
return true;
12+
13+
myMap[nums[i]] = i;
14+
i++;
15+
}
16+
return false;
17+
};
18+
19+
console.log(containsNearbyDuplicate([1,2,3,1,2,3], 2))
20+
console.log(containsNearbyDuplicate([1,2,3,1], 3))
21+
console.log(containsNearbyDuplicate([1,0,1,1], 1))
22+
console.log(containsNearbyDuplicate([1,1,1,1], 2))
23+
console.log(containsNearbyDuplicate([99,99], 2))
24+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Contains Duplicate II
2+
3+
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.
4+
5+
## Example 1
6+
7+
Input: nums = [1,2,3,1], k = 3
8+
9+
Output: true
10+
11+
## Example 2
12+
13+
Input: nums = [1,0,1,1], k = 1
14+
15+
Output: true
16+
17+
## Example 3
18+
19+
Input: nums = [1,2,3,1,2,3], k = 2
20+
21+
Output: false
22+
23+
## More Info
24+
25+
<https://leetcode.com/problems/contains-duplicate-ii/>

0 commit comments

Comments
 (0)
0