Snippets

Page

Here I leave some snippets about solving more regular well known problems. I am sure there’s more than way to solve this kind of problems, this is how I solved them, anyway, it’s just one approach. Enjoy!

Get the name with more votes. If some have the same number of votes, sort them alphabetically and the last one is the winner.

$votes =['alex', 'michael', 'harry', 'dave', 'michael', 'victor', 'harry', 'alex', 'mary', 'mary'];
echo verifyItems($votes);

function verifyItems($votes) {
sort($votes);
$sort_num_votes = [];

$aux_name = '';
for($i=0; $i<=count($votes); $i++ ){
if ( $votes[$i] != $aux_name ){
if ( $i>0 ){
$temp = ['num' => $num_votes, 'name' => $aux_name];
array_push($sort_num_votes, $temp);
}

$aux_name = $votes[$i];
$num_votes = 0;
}
$num_votes++;
}
sort($sort_n$winer =es);
$winer = end($sort_num_votes);
return $winer['name'];

}

Output: Michael