RemoveDuplicatesFromSortedArray   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 2
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A removeDuplicates2() 0 13 4
A removeDuplicates() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class RemoveDuplicatesFromSortedArray
8
{
9
    public static function removeDuplicates(array &$nums): int
10
    {
11
        if (($n = count($nums)) === 0) {
12
            return 0;
13
        }
14
        $k = 0;
15
        for ($i = 1; $i < $n; $i++) {
16
            if ($nums[$i] === $nums[$i - 1]) {
17
                $k++;
18
            } else {
19
                $nums[$i - $k] = $nums[$i];
20
            }
21
        }
22
23
        return $n - $k;
24
    }
25
26
    public static function removeDuplicates2(array &$nums): int
27
    {
28
        if (($n = count($nums)) === 0) {
29
            return 0;
30
        }
31
        [$i, $j] = [1, 0];
32
        for (; $i < $n; $i++) {
33
            if ($nums[$i] !== $nums[$j]) {
34
                $nums[++$j] = $nums[$i];
35
            }
36
        }
37
38
        return $j + 1;
39
    }
40
}
41