ContainsDuplicateII::containsNearbyDuplicate()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 14
rs 9.2222
cc 6
nc 4
nop 2
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