ContainsDuplicate   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 24
c 2
b 0
f 0
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A containsDuplicate2() 0 13 4
A containsDuplicate3() 0 14 4
A containsDuplicate() 0 15 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ContainsDuplicate
8
{
9
    public static function containsDuplicate(array $nums): bool
10
    {
11
        if (empty($nums)) {
12
            return false;
13
        }
14
        $n = count($nums);
15
        for ($i = 0; $i < $n; $i++) {
16
            for ($j = $i + 1; $j < $n; $j++) {
17
                if ($nums[$i] === $nums[$j]) {
18
                    return true;
19
                }
20
            }
21
        }
22
23
        return false;
24
    }
25
26
    public static function containsDuplicate2(array $nums): bool
27
    {
28
        if (empty($nums)) {
29
            return false;
30
        }
31
        sort($nums);
32
        for ($i = 1, $n = count($nums); $i < $n; $i++) {
33
            if ($nums[$i] === $nums[$i - 1]) {
34
                return true;
35
            }
36
        }
37
38
        return false;
39
    }
40
41
    public static function containsDuplicate3(array $nums): bool
42
    {
43
        if (empty($nums)) {
44
            return false;
45
        }
46
        $map = [];
47
        foreach ($nums as $key => $num) {
48
            if (isset($map[$num])) {
49
                return true;
50
            }
51
            $map[$num] = $key;
52
        }
53
54
        return false;
55
    }
56
}
57