| Total Complexity | 12 |
| Total Lines | 43 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class MajorityElement |
||
| 8 | { |
||
| 9 | public static function majorityElement(array $nums): int |
||
| 10 | { |
||
| 11 | [$res, $map, $cnt] = [0, [], count($nums)]; |
||
| 12 | if (empty($nums)) { |
||
| 13 | return $res; |
||
| 14 | } |
||
| 15 | foreach ($nums as $num) { |
||
| 16 | $map[$num] = isset($map[$num]) ? ++$map[$num] : 1; |
||
| 17 | if ($map[$num] > $cnt / 2) { |
||
| 18 | $res = $num; |
||
| 19 | break; |
||
| 20 | } |
||
| 21 | } |
||
| 22 | |||
| 23 | return $res; |
||
| 24 | } |
||
| 25 | |||
| 26 | public static function majorityElement2(array $nums): int |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function majorityElement3(array $nums): int |
||
| 50 | } |
||
| 51 | } |
||
| 52 |