Ad
Code
Diff
  • function closestToZero(array $ints) {
      if (0 === count($ints)) {
        return 0;
      }
      
      // Get start value
      $closest = max($ints);
      
      foreach($ints as $int) {
        if (abs($int) === abs($closest)) {
          $closest = max($int, $closest);
        }
        
        // Get min value between abs($closest) and abs($int)
        $closest = abs($int) < abs($closest) ? $int : $closest;
      }
      
      return $closest;
    }
    ?>
    • function closestToZero(array $ints) {
    • if (0 === count($ints)) {
    • return 0;
    • }
    • // Get start value
    • $closest = max($ints);
    • foreach($ints as $int) {
    • if (abs($int) === abs($closest)) {
    • $closest = max($int, $closest);
    • }
    • // Get min value between abs($closest) and abs($int)
    • $closest = abs($int) < abs($closest) ? $int : $closest;
    • }
    • return $closest;
    • }
    • ?>