Skip to content

Commit 2313e0f

Browse files
committed
Contains Duplicate II
1 parent c28406f commit 2313e0f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Contains Duplicate II.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*Given an array of integers and an integer k, find out whether there are two distinct indices i and j
2+
in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.*/
3+
4+
/**
5+
* @param {number[]} nums
6+
* @param {number} k
7+
* @return {boolean}
8+
*/
9+
var containsNearbyDuplicate = function(nums, k) {
10+
dict = {}
11+
for(i=0; i<nums.length; i++){
12+
if(nums[i] in dict){
13+
if(i-dict[nums[i]]<=k)
14+
return true
15+
}
16+
dict[nums[i]] = i
17+
}
18+
return false
19+
};

0 commit comments

Comments
 (0)