CheckIfANumberIsMajorityElementInASortedArrayEasy   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A isMajorityElement() 0 17 6
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