-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdupNums.js
More file actions
26 lines (22 loc) · 786 Bytes
/
dupNums.js
File metadata and controls
26 lines (22 loc) · 786 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
// We have a list of integers, where:
//
// The integers are in the range 1..n1..n
// The list has a length of n+1n+1
// It follows that our list has at least one integer which appears at least twice. But it may have several duplicates, and each duplicate may appear more than twice.
//
// Write a function which finds an integer that appears more than once in our list. (If there are multiple duplicates, you only need to find one of them.)
// O(n)
const findDupsFast = nums => {
let cache = {};
let dup;
for (let i = 0; i < nums.length; i++) {
if (cache.hasOwnProperty(nums[i])) {
return nums[i];
}
cache[nums[i]] = true;
}
return false;
};
const numbers = [1, 2, 3, 4, 5, 6, 7, 3, 8, 9, 10];
const result = findDupsFast(numbers);
console.log(result);