ContainsDuplicateII   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 2
b 0
f 0
dl 0
loc 16
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A containsNearbyDuplicate() 0 14 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ContainsDuplicateII
8
{
9
    public static function containsNearbyDuplicate(array $nums, int $k): bool
10
    {
11
        if (empty($nums) || $k <= 0) {
12
            return false;
13
        }
14
        $map = [];
15
        foreach ($nums as $key => $num) {
16
            if (isset($map[$num]) && $key - $map[$num] <= $k) {
17
                return true;
18
            }
19
            $map[$num] = $key;
20
        }
21
22
        return false;
23
    }
24
}
25