0

The array of natural numbers (up to and including n), passed to "findMissed" function. The numbers in array are unordered and each number occurs once. Ð’ut one number is missing. I need to determine which one is missed and return it. For the computational complexity to be O(n).

Can I use JS sort() function in solution, and then "for"

function findMissed(arr) {
  arr.sort();
  for (let i = 0; i <= arr.length; i++) {
    ...
  }
}

or it will be more complex than O(n)?

Will Black
  • 350
  • 2
  • 17
  • Yes you can. But the javascript `sort` function does not work the way you think it does. Check the section about comparing numbers https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Andrew Jun 23 '22 at 18:27
  • 1
    Sort is in general `O(n log(n))` and therefore is not `O(n)`. See the linked story for how to do it. – btilly Jun 23 '22 at 18:30
  • If each element is unique, I would create two sets and return the difference. Here are a few ways: https://www.w3docs.com/snippets/javascript/how-to-get-the-difference-between-two-arrays-in-javascript.html – thinkgruen Jun 23 '22 at 18:31

0 Answers0