-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.js
45 lines (40 loc) · 908 Bytes
/
Solution.js
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
41
42
43
44
45
/**
* @param {number[]} nums
* @return {number[]}
*/
var findErrorNums = function(nums) {
var ans = [], i, length = nums.length, s = new Set();
for (i = 0; i < length; i++) {
if (s.has(nums[i])) ans.push(nums[i]);
s.add(nums[i]);
}
for (i = 1; i <= length; i++) {
if (!s.has(i)) ans.push(i);
}
return ans;
};
/**
* @param {number[]} nums
* @return {number[]}
*/
var findErrorNums = function(nums) {
var ans = [], i, length = nums.length, prev;
nums.sort(function(a, b) {
return a - b;
});
for (i = 0; i < length; i++) {
if (i > 0 && prev === nums[i]) {
ans.push(nums[i]);
break;
}
prev = nums[i];
}
nums.splice(i, 1);
for (i = 0; i < length; i++) {
if (nums[i] !== i + 1) {
ans.push(i + 1);
break;
}
}
return ans;
};