A while ago, I signed up at https://www.codewars.com/ to practice both my PHP and JS skills. I decided to publish my results and to compare them with the best practice solution if found, thus, here we go.
Task:
- Given an array with at least three numbers.
- All numbers are equal except one.
- Find the unique number.
My solution:
<?php
function find_uniq(array $array): int {
sort($array);
if ( $array[0] === $array[1] ) rsort($array);
return $array[0];
}
Best practice:
<?php
function find_uniq($a) {
sort($a);
return ($a[0] === $a[1]) ? end($a) : current($a);
}
Resources: