-
Notifications
You must be signed in to change notification settings - Fork 13
/
oddOccurrencesInArray.php
54 lines (53 loc) · 984 Bytes
/
oddOccurrencesInArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
var_dump(solution([3, 3, 9, 9, 6]));
function solution($A){
$count = array();
foreach($A as $key=>$value){
if(!isset($count[$value])){
$count[$value]=1;
}
else if( $count[$value]===1 ){
unset($count[$value]);
}
}
return key($count);
/*
* Solution 3 - correctness 80%, performance 20%
$array=$A;
$checkedArray=array();
foreach( $array as $k=>$v ){
if( in_array($v, $checkedArray) ){
continue;
}
if(($key = array_search($v, $array )) !== false){
unset($array[$key]);
$checkedArray[]=$v;
}
if(($key = array_search($v, $array )) !== false){
unset($array[$key]);
}
else {
return $v;
}
}
*/
/*
* Solution 2 - 80%, 25% performance
foreach( $array as $k=>$v ){
$keys = array_keys( $array, $v);
var_dump($keys);
if(count($keys)===1){
return $v;
}
}
*/
/*
* Solution 1: 80% correct, 50% performance => 66%
* $array=array_count_values($A);
foreach($array as $k=>$v){
if($v==1){
return $k;
}
}*/
}
?>