<?php
function migratoryBirds($n, $ar) {
$passaros = [];
foreach ($ar as $passaro){
$passaros[$passaro] = ($passaros[$passaro] ?? 0) + 1;
}
$maiorContagem = 0;
$menorId = $ar[0];
foreach ($passaros as $id => $contagem) {
if ($contagem > $maiorContagem || ($contagem == $maiorContagem && $id < $menorId)){
$maiorContagem = $contagem;
$menorId = $id;
}
}
return $menorId;
}
$arr = [5,5,5,3,3,3];
$arr = [3,4,2,5,4];
PD9waHAKCmZ1bmN0aW9uIG1pZ3JhdG9yeUJpcmRzKCRuLCAkYXIpIHsgCiAgICAkcGFzc2Fyb3MgPSBbXTsKICAgIGZvcmVhY2ggKCRhciBhcyAkcGFzc2Fybyl7CiAgICAgICAgJHBhc3Nhcm9zWyRwYXNzYXJvXSA9ICgkcGFzc2Fyb3NbJHBhc3Nhcm9dID8/IDApICsgMTsKICAgIH0KICAgIAogICAgJG1haW9yQ29udGFnZW0gPSAwOwogICAgJG1lbm9ySWQgPSAkYXJbMF07CiAgICBmb3JlYWNoICgkcGFzc2Fyb3MgYXMgJGlkID0+ICRjb250YWdlbSkgewogICAgICAgIGlmICgkY29udGFnZW0gPiAkbWFpb3JDb250YWdlbSB8fCAoJGNvbnRhZ2VtID09ICRtYWlvckNvbnRhZ2VtICYmICRpZCA8ICRtZW5vcklkKSl7CiAgICAgICAgICAgICRtYWlvckNvbnRhZ2VtID0gJGNvbnRhZ2VtOwogICAgICAgICAgICAkbWVub3JJZCA9ICRpZDsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuICRtZW5vcklkOwp9CgokYXJyID0gWzUsNSw1LDMsMywzXTsKdmFyX2R1bXAobWlncmF0b3J5QmlyZHMoY291bnQoJGFyciksJGFycikpOwokYXJyID0gIFszLDQsMiw1LDRdOwp2YXJfZHVtcChtaWdyYXRvcnlCaXJkcyhjb3VudCgkYXJyKSwkYXJyKSk7Cg==