Skip to content

Latest commit

 

History

History
42 lines (29 loc) · 840 Bytes

find-missing-numbers.md

File metadata and controls

42 lines (29 loc) · 840 Bytes

Find missing numbers 7 Kyu

LINK TO THE KATA - ARRAYS FUNDAMENTALS

Description

You will get an array of numbers.

Every preceding number is smaller than the one following it.

Some numbers will be missing, for instance:

[-3,-2,1,5] //missing numbers are: -1,0,2,3,4

Your task is to return an array of those missing numbers:

[-1,0,2,3,4]

Solution

const findMissingNumbers = nums => {
  const minNumber = Math.min(...nums)
  const maxNumber = Math.max(...nums)

  const missingNumbers = []

  for (let i = minNumber + 1; i < maxNumber; i++) {
    if (nums.indexOf(i) === -1) missingNumbers.push(i)
  }

  return missingNumbers
}