Skip to content

Instantly share code, notes, and snippets.

@doganoo
Created November 28, 2018 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doganoo/f2d7bc6533313b665c97db12ff5c97d8 to your computer and use it in GitHub Desktop.
Save doganoo/f2d7bc6533313b665c97db12ff5c97d8 to your computer and use it in GitHub Desktop.
second highest value
//assumes that you have installed PHPAlgorithms properly:
//https://github.com/doganoo/PHPAlgorithms
$arr = [1, 2, 3, 4, 5, 6, 7, 8];
echo secondHighest($arr); //echo's 7
function secondHighest(array $array): int {
$maxHeap = new MaxHeap();
foreach ($array as $value) {
$maxHeap->insert($value);
}
print_r($maxHeap->getHeap());
return $maxHeap->getHeap()[1] > $maxHeap->getHeap()[2] ? $maxHeap->getHeap()[1] : $maxHeap->getHeap()[2];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment