Passed
Push — master ( 7e68e3...e32f0a )
by Daniel
17:36 queued 23s
created

altfindIt()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
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
    
17
    $count = 0; 
18
    $arrCount = count($arr);
19
    for ($i = 0;  $i < $arrCount; $i++)  
20
    { 
21
          
22
        for ($j = 0; $j < $arrCount; $j++) 
23
        { 
24
            if ($arr[$i] == $arr[$j]) { 
25
                $count++;
26
            } 
27
        } 
28
        if ($count % 2 != 0) { 
29
            return $arr[$i];
30
        } 
31
    } 
32
    return -1; 
33
}
34
35
// Alternate solution:
36
37
/**
38
 * altfindIt
39
 *
40
 * @param  mixed $seq
41
 * @return mixed
42
 */
43
function altfindIt(array $seq) : mixed
44
{
45
    $nums = array_count_values($seq);
46
    foreach ($nums as $key => $val) {
47
        if ($val % 2) {
48
            return $key;
49
        }
50
    }
51
    return -1; 
52
}
53