1
I am learning to program and I am doing the hackerrank algorithm exercises. I am in the migratory exercise Birds.
To access the exercise you need login access. As many do not have access, I will copy and paste the exercise here:
You have been Asked to help Study the Population of Birds migrating Across the Continent. Each type of Bird you are interested in will be identified by an integer value. Each time a particular Kind of Bird is Spotted, its id number will be Added to your array of sightings. You would like to be Able to find out which type of Bird is Most common Given a list of sightings.If two or more types of Birds are equally common, Choose the type with the smallest ID number.
Function Description
Complete the migratoryBirds Function in the editor Below. It has two Parameters:
Integer, , denoting the number of Elements in the input array. Integer Array, with array Elements denoting the respective type Numbers of each Bird in the Flock. The Function must Return an integer denoting the type number of the Most common Bird.
Raw Input Format
The first line contains an integer denoting n, the number of Birds sighted and reported in the array . The Second line describes ar as n space-separated integers Representing the type Numbers of each Bird sighted.
Personal,
my solution is this:
function migratoryBirds($n, $ar) {
$ret = [];
for ( $i = 0 ; $i < $n ; $i++ ){
$frequencia = 0;
for ( $k = 0 ; $k < $n ; $k++) {
if( $ar[ $i ] == $ar[ $k ] ) {
$ret[ $ar[ $i ] ] = 1 + $frequencia;
$frequencia++;
}
}
}
$arrayMaiorFrequencia = $ret[ $ar[0] ];
$menorChaveArrayMaiorFrequencia = $ar[0];
for ( $i = 0; $i < $n; $i++ ) {
if( $ret[ $ar[$i] ] == $arrayMaiorFrequencia && $ar[$i] < $menorChaveArrayMaiorFrequencia ) {
$menorChaveArrayMaiorFrequencia = $ar[$i];
} elseif($ret[ $ar[$i] ] > $arrayMaiorFrequencia) {
$arrayMaiorFrequencia = $ret[ $ar[$i] ];
$menorChaveArrayMaiorFrequencia = $ar[$i];
}
}
return $menorChaveArrayMaiorFrequencia;
}
The inputs of the function are defined by the exercise, i.e., $ar is an array of int and $n is the number of elements of the array.
However, I wonder if it is possible to factor this solution? Note: despite using php, and some php functions would make the solution much easier and faster, I cannot use any PHP function, only the concept of algorithms. So how do I make my solution cleaner?
But the solution has to be in php or not ? If this is the case it is appropriate to put this tag in the question. I would also like to ask that it has not been made clear to me what
$ar
? You can put avar_dump
for example ?– Isac
@Isac, it’s not necessary to be in php, but I can’t use functions. I’m using php because I started learning from it. A var_dump($ar) would be, for example, [3,4,2,5,4] and the migratoryBirds function should return 4, because it is the element that most appears (most frequently) in the array. Another example, var_dump($ar) = [5,5,5,3,3,3]. The function’s Return would be 3. Having 5 and 3 the highest frequencies, but equal frequencies, the migratoryBirds function should return the lowest value type.
– milho
@Isac, the beginning of my comment was confused.. I can use any language, but not its functions, only algorithms
– milho