javascript find missing number in array
You can do this with the use of indexOf
function:
var a = [5],
count = 5;
var missing = new Array();
for (var i = 1; i <= count; i++) {
if (a.indexOf(i) == -1) {
missing.push(i);
}
}
console.log(missing); // to check the result.
Better way to deal with dynamic MIN & MAX numbers to find range of missing number in an array
const findMissing = num => {
const max = Math.max(...num); // Will find highest number
const min = Math.min(...num); // Will find lowest number
const missing = []
for(let i=min; i<= max; i++) {
if(!num.includes(i)) { // Checking whether i(current value) present in num(argument)
missing.push(i); // Adding numbers which are not in num(argument) array
}
}
return missing;
}
findMissing([1,15]);