Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

You have created a good solution, just a few style points that can reduce code size.

  • Why null rather than false or even 0 in arr[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)) or nums.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 than false or even 0 in arr[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)) or nums.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 than false or even 0 in arr[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)) or nums.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;
}
Source Link
Blindman67
  • 22.8k
  • 2
  • 16
  • 40

You have created a good solution, just a few style points that can reduce code size.

  • Why null rather than false or even 0 in arr[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)) or nums.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;
}
lang-js

AltStyle によって変換されたページ (->オリジナル) /