Skip to content

Instantly share code, notes, and snippets.

@amaxwell01
Last active June 15, 2021 15:32
Show Gist options
  • Save amaxwell01/6108502 to your computer and use it in GitHub Desktop.
Save amaxwell01/6108502 to your computer and use it in GitHub Desktop.
How to find the missing numbers from an array of integers. Q: You’re given an array of N integers. N may be very large. You know that every integer 1-N appears once in the array, except there is one or more integer(s) missing.
/* ============================
* EXAMPLE 1:
* ============================ */
var numbers = [0,1,3,4,5,7,8]; // Missing 2,6
var lastNumber = numbers[numbers.length - 1];
var expectedSum = (lastNumber * (lastNumber + 1)) / 2;
var actualSum = 0;
// Show the difference
for (var i = 0; i < numbers.length; i++) {
actualSum += numbers[i];
}
console.log(expectedSum - actualSum);
/* ============================
* EXAMPLE 2:
* ============================ */
var numbers = [0,1,3,4,5,7,8]; // Missing 2,6
var missing = [];
// Find the missing array items
for ( var i = 0; i < numbers.length; i++ ) {
if ( (numbers[i+1] - numbers[i]) > 1 ) {
missing.push( numbers[i+1] - numbers[1] );
}
}
console.log( missing );
/* ============================
* EXAMPLE 3:
* ============================ */
var intArray = [9,1,5,8,7,4,3,0,10,13,15,19,12,16,18]; // Missing 2,6,11,14,17
var arrayLength = Math.max.apply(Math, intArray);
var missing = []
for ( var i = 0; i < arrayLength; i++ ) {
if ( intArray.indexOf(i) < 0 ) {
missing.push( i );
}
}
console.log( missing );
@rucinskic
Copy link

I don't know about the last one...as I don't know Javascript (Math.max.apply(...)). But the middle one seems to have a bug in that it cannot detect missing array indexes of multiple missing numbers next to one another. I have only done paper and pen, but...

var numbers = [1, 2, 3, 6, 7, 8];  // Missing 4, 5 ... index 3,  4

reports only index 3...but should also report index 4 (if I am right about what you are suppose to do)

In Java:

for (int i = 0;  i < numbers.length; i++) {

    int diff = numbers[i + 1] - numbers[i];
    int count = 0;

    while ((diff - count) > 1) {

        missing.push(diff + count);
        count++;

    }
}

I have not tested this...but this seems to work

@andrewdallas1
Copy link

Only tested the first example which seems to work if the array contains only single digits. For instance in this case
var numbers = [10, 11, 12, 14];
(14 * 15) / 2 = 105
10 + 11 + 12 + 14 = 47
105 - 47 = 58

@ankitarora05
Copy link

/Example Three just sort the array/

var missingNumber = function(nums) {
var out = [];
nums.sort(function(a,b){return a-b});
for(var i=0;i<nums.length;i++) {
if(nums[i+1] - nums[i] > 1) {
out.push(nums[i+1] - nums[1]);
}
}
return out;
};

@InerkyJad
Copy link

the best one-line solution I came up with so far is
missingnums = (Math.max.apply(Math, x) - Math.min.apply(Math, x) + 1) - x.length

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment