-
Notifications
You must be signed in to change notification settings - Fork 0
/
findWinners.php
31 lines (30 loc) · 980 Bytes
/
findWinners.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
class Solution {
/**
* @param Integer[][] $matches
* @return Integer[][]
*/
function findWinners($matches) {
// 0 indexed value list, l1
// 1 indexed value list, l2
$firstElements = [];
$secondElements = [];
foreach ($matches as $item) {
$firstElements[] = $item[0];
$secondElements[] = $item[1];
}
// array_count_values l2
$secondElementCountValues = array_count_values($secondElements);
// return keys which value 1
$loser = array_keys($secondElementCountValues, 1);
// make unique array l1, l2
$firstElements = array_unique($firstElements);
$secondElements = array_unique($secondElements);
// array_diff l1, l2
$winner = array_diff($firstElements, $secondElements);
// sort array then [winner, loser], result
sort($winner);
sort($loser);
return [$winner, $loser];
}
}