-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindDuplicate.js
More file actions
40 lines (32 loc) · 926 Bytes
/
FindDuplicate.js
File metadata and controls
40 lines (32 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
You must solve the problem without modifying the array nums and using only constant extra space.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums) {
let len = nums.length;
let duplicate = nums[0];
let i = 1;
let j = 1;
while (i < len) {
if (nums[i] === duplicate) {
return duplicate
} else {
i++
if (i === len) {
duplicate = nums[j]
i = j + 1
j++
}
}
}
return -1
};
/*
The algorithm above is slow for leetcodes time limit, I found out that you can use something called a "Set" to solve this much faster.
*/
console.log(findDuplicate([2,1,2]))