8000 19/07/26 · freezeYe/leetcode-js@b2300a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b2300a9

Browse files
author
zhangbo
committed
19/07/26
1 parent fd99075 commit b2300a9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

287.Array.M-FindTheDuplicateNumber.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive),
3+
* prove that at least one duplicate number must exist. Assume that there is only one duplicate number,
4+
* find the duplicate one.
5+
* Input: [1,3,4,2,2]
6+
* Output: 2
7+
*/
8+
9+
/**
10+
* @param {number[]} nums
11+
* @return {number}
12+
*/
13+
var findDuplicate = function (nums) {
14+
let left = 0, right = nums.length
15+
while (left < right) {
16+
let mid = left + Math.trunc((right - left) / 2), cnt = 0;
17+
for (let num of nums) {
18+
if (num <= mid)++cnt;
19+
}
20+
if (cnt <= mid) left = mid + 1;
21+
else right = mid;
22+
}
23+
return right;
24+
};

0 commit comments

Comments
 (0)
0