8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fd99075 commit b2300a9Copy full SHA for b2300a9
287.Array.M-FindTheDuplicateNumber.js
@@ -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