0

I need to find missing numbers in array. There are many similar questions here & i checked it all, but can't find code who works for me. The close answer to my question was:

numbers.sort((a, b) => a - b);
miss = Array.from({
            length: numbers[numbers.length - 1] - numbers[0] + 1
        },
        (_, idx) => numbers[0] + idx)
    .filter(elem => !numbers.includes(elem));
console.log(miss.join(','));

It work correctly for this cases:

numbers = [4, 3, 2, 7, 8, 2, 3, 1] // print 5, 6

numbers = [1, 2, 3, 3, 5] // print 4

But for next case:

numbers = [1, 1, 1, 1, 1, 1, 1, 1] // returns nothing

I need to return 2, 3, 4, 5, 6, 7, 8

Edit: Need to find all the integers of [1, N] inclusive that do NOT appear in this array.

6
  • 1
    How do you define your missing number? Like: Should the algorithm search for the biggest and lowest value and return all numbers, that doesn't exist in that array? Commented Dec 20, 2018 at 12:31
  • 1
    What do you mean by "missing numbers"? Commented Dec 20, 2018 at 12:32
  • What is the length of the miss array in case 3, before .filter is applied? Commented Dec 20, 2018 at 12:32
  • Looks like length is being used to determine the desired range. Commented Dec 20, 2018 at 12:32
  • Your function will return nothing for an array of any single digit array. Currently your function is moreorless taking the lowest and going to the highest. Yet.. you don't ever specify you're end number. Hence so many comments about what's defined as missing. In a range of 1 to 1 - nothing is missing. 1 is in the range... you need to change your function to have a param for endOfRange Commented Dec 20, 2018 at 12:35

4 Answers 4

1

const missingValues = (numbers) => {
  let size = numbers.length
  let result = []
  for (i = 1; i < size + 1; i++) {
    if (!numbers.includes(i)) {
      result.push(i)
    }
  }
  console.log(result.join(','));
}

let numbers = [1, 1, 1, 1, 1, 1, 1, 1] 
missingValues(numbers)


numbers = [4, 3, 2, 7, 8, 2, 3, 1] 

missingValues(numbers)

Sign up to request clarification or add additional context in comments.

Comments

1

This code will search for missing numbers, depending on the length of your given array.

function calculate(input) {
    let result = list()

    // We will start with i=1, since 0 isn't in our scope.
    // The last value in our scope should be i <= input.length
    for(i=1; i<= input.length;i++){

      // Comparing if current number of for-loop is inside given array.
      if (!input.includes(i)){

         // If that's the case, add missing number to result-list.
         result.push(i);
      }
    }

    console.log(result)
}

Comments

0

You could take 1 as minimum value and check for the length of the array if the value exist or not, then add it to the missing values.

function getMissingValues(array) {
    var v = 1,
        i = array.length,
        result = [];

    while (i--) {
        if (!array.includes(v)) result.push(v);
        ++v;
    }
    return result;
}

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

Comments

0

Checks an Array for missing numbers between 1 - 9

let ar = [1, 1, 1, 1, 1, 1, 1, 1]
let nope = []

for(i=1;i<=9;i++){
  if (!ar.includes(i)) nope.push(i)
}

console.log(nope)
console.log(nope.join(", "))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.