You have created a good solution, just a few style points that can reduce code size.
Why
null
rather thanfalse
or even0
inarr[x - 1] = null;
Why not use commas to remove need for return. Eg
.reduce((arr, x) => {arr[x - 1] = null; return arr; }
becomes.reduce((arr, x) => (arr[x - 1] = null, arr))
To create an array of indexes you could have used the shorter forms,
Array.from(nums, (item, i) => i + 1))
ornums.map((item,i) => i + 1)
##Rewrites
Rewrites
function findMissing(arr) {
return arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
}
or
const findMissing = arr => arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
You could also have solved it with a set as follows
function findMissing(arr) {
const s = new Set(arr), res = [];
var i = arr.length;
while (i) { s.has(i--) || res.push(i + 1) }
return res;
}
You have created a good solution, just a few style points that can reduce code size.
Why
null
rather thanfalse
or even0
inarr[x - 1] = null;
Why not use commas to remove need for return. Eg
.reduce((arr, x) => {arr[x - 1] = null; return arr; }
becomes.reduce((arr, x) => (arr[x - 1] = null, arr))
To create an array of indexes you could have used the shorter forms,
Array.from(nums, (item, i) => i + 1))
ornums.map((item,i) => i + 1)
##Rewrites
function findMissing(arr) {
return arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
}
or
const findMissing = arr => arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
You could also have solved it with a set as follows
function findMissing(arr) {
const s = new Set(arr), res = [];
var i = arr.length;
while (i) { s.has(i--) || res.push(i + 1) }
return res;
}
You have created a good solution, just a few style points that can reduce code size.
Why
null
rather thanfalse
or even0
inarr[x - 1] = null;
Why not use commas to remove need for return. Eg
.reduce((arr, x) => {arr[x - 1] = null; return arr; }
becomes.reduce((arr, x) => (arr[x - 1] = null, arr))
To create an array of indexes you could have used the shorter forms,
Array.from(nums, (item, i) => i + 1))
ornums.map((item,i) => i + 1)
Rewrites
function findMissing(arr) {
return arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
}
or
const findMissing = arr => arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
You could also have solved it with a set as follows
function findMissing(arr) {
const s = new Set(arr), res = [];
var i = arr.length;
while (i) { s.has(i--) || res.push(i + 1) }
return res;
}
You have created a good solution, just a few style points that can reduce code size.
Why
null
rather thanfalse
or even0
inarr[x - 1] = null;
Why not use commas to remove need for return. Eg
.reduce((arr, x) => {arr[x - 1] = null; return arr; }
becomes.reduce((arr, x) => (arr[x - 1] = null, arr))
To create an array of indexes you could have used the shorter forms,
Array.from(nums, (item, i) => i + 1))
ornums.map((item,i) => i + 1)
##Rewrites
function findMissing(arr) {
return arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
}
or
const findMissing = arr => arr
.reduce((a, i) => (a[i - 1] = 0, a), arr.map((item, i) => i + 1))
.filter(Boolean);
You could also have solved it with a set as follows
function findMissing(arr) {
const s = new Set(arr), res = [];
var i = arr.length;
while (i) { s.has(i--) || res.push(i + 1) }
return res;
}