isMajorityElement()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 9.2222
cc 6
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class CheckIfANumberIsMajorityElementInASortedArrayEasy
8
{
9
    public static function isMajorityElement(array $nums, int $target): bool
10
    {
11
        if (empty($nums) || $target <= 0) {
12
            return false;
13
        }
14
        if (($n = count($nums)) === 1) {
15
            return $nums[0] === $target;
16
        }
17
18
        $cnt = 0;
19
        foreach ($nums as $num) {
20
            if ($num === $target) {
21
                $cnt++;
22
            }
23
        }
24
25
        return $cnt * 2 > $n;
26
    }
27
}
28