Number Zoo Patrol

PHP

function find_number(array $a): int {
  sort($a);
  $len = count($a);

  for ($i = 1; $i <= $len; $i++) {
    if ($i - 1 != $a[$i - 1] - 1) {
      return $i;
    }
  }

  return $len + 1;
}

JavaScript

function findNumber(array) {
  if (array.length == 0) return 1;
  let arraySum = array.reduce((sum, val) => sum + val);
  return ((array.length + 1) * (array.length + 2) / 2) - arraySum;
}

CoffeeScript

findNumber = (array) ->
  if array.length == 0
    return 1;
  arraySum = array.reduce((sum, val) => sum + val);
  return ((array.length + 1) * (array.length + 2) / 2) - arraySum;

Tags

  1. php (Private)
  2. javascript (Private)
  3. coffeescript (Private)
  4. 6-kyu (Private)
  5. codewars (Private)
  6. answer (Private)