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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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;
}
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:
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
}
@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!
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;
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
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.
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) {
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;
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;
I don’t know how expensive this algorithm is (perhaps exponential?) but I see a similarity between many other results.
const missingNos = (arr, k) => {
let totalTiles = arr.length + k;
let solArr = [];
for (let i=1; i<=totalTiles; i++) {
if (arr.indexOf(i) === -1) {
solArr.push(i);
}
}
return solArr;
} // Have a nice day
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;