-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathsetMismatch.js
More file actions
27 lines (23 loc) · 582 Bytes
/
setMismatch.js
File metadata and controls
27 lines (23 loc) · 582 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
function findDuplicateAndMissing(nums){
const length = nums.length;
let duplicate = -1;
let missing = -1;
for (let num of nums) {
num = Math.abs(num);
if(nums[num-1]<0) {
duplicate = num;
} else {
nums[num-1] = -nums[num-1];
}
}
for (let i=0; i<length; i++) {
if(nums[i] >0) {
missing = i+1;
}
}
return [duplicate, missing];
}
const nums1 = [1,3,3,4];
const nums2 = [1,1];
console.log(findDuplicateAndMissing(nums1));
console.log(findDuplicateAndMissing(nums2));