Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
missing integers from a sorted array
  • I have an array of sorted numbers being passed into my function
  • I want to return the number of numbers I'm missing

Example:

the array I pass is [1,5,6,9]

the numbers I'm missing are 2,3,4,7,8

the counter should be 5

what I'm attempting to do:

without using any built ins. while my next number in my list is not the next number already in my list I would increment by one.


function missing(numbers){
    counter = 0;
    let j = 1;
    for(let i = 0; i<numbers.length; i++){
        while(numbers[i+1] != numbers[i]+j){
            j+=1;
            counter+=1;
            console.log("missing "+ numbers[i]+j)
        }
    }
return counter;
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I'd suggest using a Set to contain all numbers in the input array, then generating a range array of all possible numbers corresponding to that array.

We do this by first getting the min and max of our input array, then generating the range from min to max.

Then we use Array.filter() to remove all items in the range array in our original array (in the Set)

function getMissingNumbers(arr) {
    const [min,max] = [Math.min(...arr), Math.max(...arr)];
    const range = Array.from({ length: (max - min + 1)}, (v,k) => k + min);
    const nSet = new Set(arr);
    return range.filter(n => !nSet.has(n));
}

console.log(getMissingNumbers([1,5,6,9]))
console.log(getMissingNumbers([1,5]))
console.log(getMissingNumbers([2,3,4,5,8,9]))
.as-console-wrapper { max-height: 100% !important; top: 0; }

And a slightly simpler version (assuming arrays are sorted):

function getMissingNumbers(arr) {
    let s = new Set(arr);
    let range = Array.from({ length: (arr[arr.length-1] - arr[0])}, (v,k) => k + arr[0]);
    return range.filter(n => !s.has(n));
}

console.log(getMissingNumbers([1,5,6,9]))
console.log(getMissingNumbers([1,5]))
console.log(getMissingNumbers([2,3,4,5,8,9]))
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

function missing(numbers){
    counter = 0;
  
    for(let i = 0; i<numbers.length; i++){
        let j = 1;
        while(numbers[i+1] != numbers[i]+j && numbers[i]+j < 10){
            console.log(`missing + ${numbers[i]+j}`, numbers[i], j)
            j+=1;
            counter+=1;
        }
    }
    return counter;
}

There are some issues with your logic.

  1. First, you're expecting missing numbers till 9, which is missing from your while condition.
  2. You're not resetting the j variable once you're out of the while loop.
  3. You're incrementing j before the print statement giving you wrong values which could've failed the while condition.
about 4 years ago · Juan Pablo Isaza Report

0

You could take a single loop and run from index zero to the end of the given array.

At the same time use an variable for the increasing value and if not in the array push this value to the result set of missing values.

function getMissing(array) {
    let i = 0,
        v = array[0],
        result = [];
    
    while (i < array.length) {
        if (v === array[i]) {
            i++;
            v++;
            continue;
        }
        result.push(v++);
    }
    return result;
}


console.log(...getMissing([1, 5, 6, 9])); // 2 3 4 7 8

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!