DEV Community

Discussion on: Daily Challenge #117 - MinMinMax

Collapse
 
mercatante profile image
Steven Mercatante
function minMinMax(arr) {
  // Sorting `arr` lets us easily select the min and max values
  arr.sort((a, b) => a - b)
  const min = arr[0]
  const max = arr[arr.length - 1]

  // Starting at the `min` number, search between 
  // `min` and `max` until you find a value that's not in `arr`
  let minimumAbsent = null
  for (let i = min; i < max; i++) {
    if (!arr.includes(i)) {
      minimumAbsent = i
      break
    }
  }

  return [min, minimumAbsent, max]
}

minMinMax([-1, 4, 5, -23, 24]); //[-23, -22, 24]
// minMinMax([1, 3, -3, -2, 8, -1]); //[-3, 0, 8]
// minMinMax([2, -4, 8, -5, 9, 7]); //[-5, -3, 9]

Runnable example: