-
Notifications
You must be signed in to change notification settings - Fork 0
/
[03-w1-d3]findMissingNums.js
51 lines (47 loc) · 1.18 KB
/
[03-w1-d3]findMissingNums.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
46
47
48
49
50
51
// Learning: for loop, push method, sort.
// Language: javascript
//problem:
//write a function that takes an array of numbers and returns a new array of the missing numbers.
//Examples:
//findMissingNums([1,5,6,9]); // [2,3,4,7,8]
//solution:
function findMissingNums(arr) {
const missingNums = [];
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
if (arr[i +1 ] - arr[i] > 1) {
for (let x = arr[i] + 1; x < arr[i+1]; x++) {
missingNums.push(x);
}
}
}
return missingNums;
}
function findMissingNums(arr) {
const missingNums = [];
arr.sort((a, b) => a - b);
let l = arr[0]
let r = arr[arr.length - 1]
for (let i = l; i <= r; i++) {
if (!arr.includes(i)) {
missingNums.push(i)
}
}
return missingNums;
}
function findMissingNums(arr) {
let obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i]] = true;
}
let missingNums = [];
arr.sort((a, b) => a - b);
let l = arr[0]
let r = arr[arr.length - 1]
for (let i = l; i <= r; i++) {
if (!obj[i]) {
missingNums.push(i)
}
}
return missingNums;
}