findIt()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 7
nop 1
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/*
3
Given an array, find the int that appears an odd number of times.
4
5
There will always be only one integer that appears an odd number of times.
6
*/
7
8
/**
9
 * findIt
10
 *
11
 * @param  mixed $arr
12
 * @return int
13
 */
14
function findIt(array $arr) : int
15
{
16
    $count = 0;
17
    $arrCount = count($arr);
18
    for ($i = 0;  $i < $arrCount; $i++) {
19
        for ($j = 0; $j < $arrCount; $j++) {
20
            if ($arr[$i] == $arr[$j]) {
21
                $count++;
22
            }
23
        }
24
        if ($count % 2 != 0) {
25
            return $arr[$i];
26
        }
27
    }
28
    return -1;
29
}
30
31
// Alternate solution:
32
33
/**
34
 * altfindIt
35
 *
36
 * @param  mixed $seq
37
 * @return mixed
38
 */
39
function altfindIt(array $seq) : mixed
40
{
41
    $nums = array_count_values($seq);
42
    foreach ($nums as $key => $val) {
43
        if ($val % 2) {
44
            return $key;
45
        }
46
    }
47
    return -1;
48
}
49