MissingInteger   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B solution() 0 31 6
1
<?php
2
3
namespace Lesson02;
4
5
class MissingInteger
6
{
7
    public function solution($A)
8
    {
9
        $uniquePositives = array_unique(array_filter($A, function ($number) {
10
            return $number > 0;
11
        }));
12
13
        $missingNumber = 1;
14
        $count = count($uniquePositives);
15
16
        if ($count > 0) {
17
            if ($count > 1) {
18
                asort($uniquePositives);
19
            }
20
21
            $sortedUniques = array_values($uniquePositives);
22
            $sortedUniquesCount = count($sortedUniques);
23
24
            for ($i = 0; $i < $sortedUniquesCount; $i++) {
25
                if ($sortedUniques[$i] > ($i + 1)) {
26
                    $missingNumber = $i + 1;
27
                    break;
28
                }
29
            }
30
31
            if ($i == $sortedUniquesCount) {
32
                $missingNumber = $i + 1;
33
            }
34
        }
35
36
        return $missingNumber;
37
    }
38
}
39