Tasks Details
easy
Find value that occurs in odd number of elements.
Task Score
66%
Correctness
100%
Performance
25%
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9
- the elements at indexes 0 and 2 have value 9,
- the elements at indexes 1 and 3 have value 3,
- the elements at indexes 4 and 6 have value 9,
- the element at index 5 has value 7 and is unpaired.
Write a function:
function solution($A);
that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9the function should return 7, as explained in the example above.
Write an efficient algorithm for the following assumptions:
- N is an odd integer within the range [1..1,000,000];
- each element of array A is an integer within the range [1..1,000,000,000];
- all but one of the values in A occur an even number of times.
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used PHP
Time spent on task 7 minutes
Notes
not defined yet
Task timeline
Code: 16:29:22 UTC,
java,
autosave
Code: 16:29:32 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:30:24 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:30:41 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:30:56 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:31:19 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:32:14 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null ) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:32:25 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:32:37 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:32:53 UTC,
php,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
User test case 1:
[2, 2, 2, 3, 3, 5, 7, 5, 7]
Analysis
Code: 16:33:14 UTC,
php,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
User test case 1:
[2, 2, 2, 3, 3, 5, 7, 5, 7]
User test case 2:
[]
User test case 3:
[1]
User test case 4:
[4, 5]
User test case 5:
[1, 2, 1]
Analysis
Code: 16:33:44 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
} e
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:34:00 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
} else print "debug\n";
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:34:01 UTC,
php,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
} else print "debug\n";
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
User test case 1:
[2, 2, 2, 3, 3, 5, 7, 5, 7]
User test case 2:
[]
User test case 3:
[1]
User test case 4:
[4, 5]
User test case 5:
[1, 2, 1]
Analysis
expand all
User tests
1.
0.004 s
OK
function result: 2
function result: 2
stdout:
debug debug debug debug
1.
0.004 s
OK
function result: 0
function result: 0
1.
0.004 s
OK
function result: 1
function result: 1
1.
0.004 s
OK
function result: 0
function result: 0
1.
0.004 s
OK
function result: 2
function result: 2
stdout:
debug
Code: 16:34:46 UTC,
php,
autosave
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
} else
print "debug\n";
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Code: 16:34:54 UTC,
php,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
//if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
// } else
print "debug\n";
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
User test case 1:
[2, 2, 2, 3, 3, 5, 7, 5, 7]
User test case 2:
[]
User test case 3:
[1]
User test case 4:
[4, 5]
User test case 5:
[1, 2, 1]
Analysis
expand all
Example tests
1.
0.004 s
OK
stdout:
debug debug debug debug debug debug debug
expand all
User tests
1.
0.004 s
OK
function result: 2
function result: 2
stdout:
debug debug debug debug debug debug debug debug debug
1.
0.004 s
OK
function result: 0
function result: 0
1.
0.004 s
OK
function result: 1
function result: 1
stdout:
debug
1.
0.004 s
OK
function result: 0
function result: 0
stdout:
debug debug
1.
0.004 s
OK
function result: 2
function result: 2
stdout:
debug debug debug
Code: 16:35:09 UTC,
php,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
User test case 1:
[2, 2, 2, 3, 3, 5, 7, 5, 7]
User test case 2:
[]
User test case 3:
[1]
User test case 4:
[4, 5]
User test case 5:
[1, 2, 1]
Analysis
Code: 16:35:27 UTC,
php,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
User test case 1:
[2, 2, 2, 3, 3, 5, 7, 5, 7]
User test case 2:
[]
User test case 3:
[1]
User test case 4:
[4, 5]
User test case 5:
[1, 2, 1]
Analysis
Code: 16:35:30 UTC,
php,
final,
score: 
66
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$result = 0;
$N = sizeof($A);
if ($N > 1000000) return $result;
for ($i = 0 ; $i < $N ; $i++) {
if ($A[$i] != null && $N > 0) {
for ($j = $N-1 ; $j > $i ; $j--) {
if ($A[$j] != null && $A[$i] == $A[$j]) {
$A[$i] = null;
$A[$j] = null;
break;
}
}
}
}
$A = array_filter($A);
if (sizeof($A) == 1) {
foreach($A as $a) $result = $a;
}
return $result;
}
Analysis summary
The following issues have been detected: timeout errors.
Analysis
Detected time complexity:
O(N**2)
expand all
Performance tests
1.
0.048 s
OK
medium2
medium random test n=100,003
medium random test n=100,003
✘
TIMEOUT ERROR
running time: >6.00 sec., time limit: 0.13 sec.
running time: >6.00 sec., time limit: 0.13 sec.
1.
6.000 s
TIMEOUT ERROR,
running time: >6.00 sec., time limit: 0.13 sec.
big1
big random test n=999,999, multiple repetitions
big random test n=999,999, multiple repetitions
✘
TIMEOUT ERROR
running time: >6.00 sec., time limit: 0.99 sec.
running time: >6.00 sec., time limit: 0.99 sec.
1.
6.000 s
TIMEOUT ERROR,
running time: >6.00 sec., time limit: 0.99 sec.
1.
7.000 s
TIMEOUT ERROR,
running time: >7.00 sec., time limit: 1.15 sec.