JavaScript Challenge - Find the Missing Numbers

This community-built FAQ covers the “Find the Missing Numbers” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the JavaScript challenge Find the Missing Numbers

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

It wasn’t clear to me if the input array was sorted or not so this will handle either case. The example talks about a bag of random numbers so that seems like unsorted but example input arrays show numbers in order. I am guessing this is O(N) but open for input on that…

function missingNos(array, k) {
  let nrMap = new Map();
  array.forEach((k) => nrMap.set(k, true));
  let missingNrs = [];
  let nr = 1;
  while (missingNrs.length < k) {
    if (!nrMap.get(nr)) {
      missingNrs.push(nr)
    }
    nr++;
  }
  return missingNrs;
}
2 Likes
function missingNos(array, k) {
  var missingNumbers = [];
  for (var i = array[0]; i <= array[array.length - 1]; i++) {
    if (array.indexOf(i) === -1) missingNumbers.push(i);
  }
  return missingNumbers;
}

const testArray = [1, 2, 4, 5, 6, 7, 8, 10];
console.log(missingNos(testArray, 2));

// Leave this so we can test your code:

2 Likes

I like the use of the while loop here. It prevents looping over the remaining numbers if the missing ones have already been found.

2 Likes

Here’s my first pass at it. Not the most efficient, but gets the job done.

function missingNos(array, k) {
  const n = array.length + k;
  const allNums = [...Array(n).keys()].map((i) => i + 1);
  return allNums.filter((e) => !array.includes(e));
}
2 Likes
function missingNos(array, k) {
let add = 1, limit = k;
let arr = []
for (let i = 0; i < (array.length + k) && limit != 0; i++)
{
  let a = array[i], b = array[i - 1];
  if (a === undefined) {a = array[i - 1] + 1+ limit;}    
  let correct = a - b;
  if (b === undefined) {correct = array[i]}
  if (add === array[i])
  {
    add++;
  } 
  else if (correct > 1)
  { 
    let stop = correct;
    do
    {
      stop--;      
      limit--;    
      arr = [...arr, add++]
    } while (stop != 1)
    if (b != undefined){add = array[i] + 1;}
    else if(b === undefined){add++;}
  }  
}
return arr;
}

console.log(missingNos([2, 4, 5, 8], 10))

No methods way. It works XD.

javascript solution:

function missingNos(array, k) {
let miss=;
let i=1;
while(miss.length < k) {
if(!array.includes(i)) {
miss.push(i);
}
i++;
}

return miss;
}

const testArray = [1, 2, 4, 5, 6, 7, 8, 10];
console.log(missingNos(testArray, 2));

// Leave this so we can test your code:
module.exports = missingNos;

Not the most elegant of solutions, but I included a check to stop the loop once k is reached so it makes me happy

function missingNos(array, k) { const last = array[array.length - 1] let result = [] for(let i = 1; i <= last; i++) { if(result.length === k) { return result } if (!array.includes(i)) { result.push(i) } } return result }
function missingNos(array, k) {

const newarr = [];
const missing = []

for (let i = 1; i<=array.length+k; i++){
  newarr.push(i);
}

for(num of newarr){
  if (!array.includes(num)){
    missing.push(num);
  } else continue;
}

return missing
}

const testArray = [1, 2, 4, 5, 6, 7, 8, 10];
console.log(missingNos(testArray, 2));
function missingNos(array, k) { const newarr = []; const missing = [] for (let i = 1; i<=array.length+k; i++){ newarr.push(i); } for(num of newarr){ if (!array.includes(num)){ missing.push(num); } else continue; } return missing } const testArray = [1, 2, 4, 5, 6, 7, 8, 10]; console.log(missingNos(testArray, 2));

@creatively_rose Nice! Instead of working out the last element of the given array, I used the array length + k. It looks like we had a similar solution, otherwise!

This uses sets to prevent duplicates…

function missingNos(array, k=0) { const a = new Set(array) const b = new Set() for (let i = 1; i <= a.size + k; i++) { if (! a.has(i)) { b.add(i) } } return Array.from(b).sort((p, q) => p - q) } console.log(missingNos([2, 3, 4, 5, 6, 7, 8, 9], 2)) console.log(missingNos([], 2)) console.log(missingNos([])) console.log(missingNos([2, 3, 4, 5, 6, 7, 8, 9], 6)) console.log(missingNos([8, 9], 4)) // Leave this so we can test your code: module.exports = missingNos;

The last test case is a deliberate fail.

1 Like

Nice way! But is the .sort needed in the end? I think the JS set maintains insertion order already (perhaps different from some other languages).

1 Like

Yeah, I wanted to look into that more. Was also thinking of unordered sequence being passed in which would mess with the end result. If we can trust the inputs, then the sort shouldn’t be needed

1 Like

Yes. It will be faster with the .sort omitted for sure. And I think since you are generating the b set based on i starting at 1 and incrementing it seems like it should still handle an unsorted input array without the sort.

1 Like

Good point, and even that crossed my mind. Was probably overthinking it. Going to see if it will still pass. BRB.


Yup, it still passes.

function missingNos(array, k=0) {
  const a = new Set(array)
  const b = new Set()
  for (let i = 1; i <= a.size + k; i++) {
    if (! a.has(i)) {
      b.add(i)
    }
  }
  return Array.from(b)
}
console.log(missingNos([2, 3, 4, 5, 6, 7, 8, 9], 2))
console.log(missingNos([], 2))
console.log(missingNos([]))
console.log(missingNos([2, 3, 4, 5, 6, 7, 8, 9], 6))
console.log(missingNos([8, 9], 4))
// Leave this so we can test your code:
module.exports = missingNos;

function missingNos(array, k) {
const arrayLength = array.length + k;
const pustaArray =
for(let i=0; i<arrayLength;i++){
if(array[i] != i+1){
array.splice(i,0,i+1);
pustaArray.push(i+1)
}
}
return pustaArray
}

const testArray = [1, 2, 4, 5, 6, 7, 8, 10];
console.log(missingNos(testArray, 2));

// Leave this so we can test your code:
module.exports = missingNos;

function missingNos(array, k) {
  let result = [];
  let arrayEntire = new Array(array.length + k);
  for (let i=1; i<= arrayEntire.length; i++) { 
    arrayEntire.fill(i, i - 1); 
    }
  let arraySet = new Set(array);
  arrayEntire.forEach(function(element) {
    if (!arraySet.has(element)) {
      result.push(element);
    }
  })
  return result;
}

// Leave this so we can test your code:
module.exports = missingNos;

Using a hashmap:

function missingNos(array, k) { // Creating a hashmap let n = array.length; let result = []; let d = new Map(); // Iterate over array for (let i = 0; i < n; i++) d.set(array[i], array[i]); let cnt = 1; let fl = 0; // Iterate to find missing // element for (let i = 0; i < (n + k); i++) { if (!d.has(cnt)) { fl += 1; result.push(cnt); if (fl == k) break; } cnt += 1; } return result; } const testArray = [1, 2, 4, 5, 6, 7, 8, 10]; console.log(missingNos(testArray, 2)); // Leave this so we can test your code: module.exports = missingNos;
function missingNos(array, k) {
  let originArr = Array.from({length:array.length + k}, (v, i) => i + 1);
  return originArr.filter(v => !(array.includes(v)));
}