Skip to content

Commit 0fc29b1

Browse files
authored
Update 287-find-the-duplicate-number.js
1 parent 719e10e commit 0fc29b1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

287-find-the-duplicate-number.js

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const findDuplicate = function(nums) {
6+
const len = nums.length
7+
if(len > 0) {
8+
let slow = nums[0]
9+
let fast = nums[nums[0]]
10+
while(slow !== fast) {
11+
slow = nums[slow]
12+
fast = nums[nums[fast]]
13+
}
14+
slow = 0
15+
while(slow !== fast) {
16+
slow = nums[slow]
17+
fast = nums[fast]
18+
}
19+
return slow
20+
}
21+
return -1;
22+
};
23+
24+
// another
25+
126
/**
227
* @param {number[]} nums
328
* @return {number}

0 commit comments

Comments
 (0)