Tasks Details
easy
Find value that occurs in odd number of elements.
Task Score
55%
Correctness
80%
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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 19 minutes
Notes
not defined yet
Code: 08:05:45 UTC,
java,
autosave
Code: 08:16:54 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.080 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{ '3': NaN, '7': NaN, '9': NaN }
Code: 08:17:23 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.056 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{ '3': NaN, '7': NaN, '9': NaN }
Code: 08:17:51 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.056 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{ '3': NaN, '7': NaN, '9': NaN }
Code: 08:18:29 UTC,
js,
autosave
Code: 08:18:45 UTC,
js,
autosave
Code: 08:18:57 UTC,
js,
autosave
Code: 08:19:06 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
console.log(classifiedNums)
}
Analysis
expand all
Example tests
1.
0.056 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{ '3': 2, '7': 1, '9': 4 }
Code: 08:19:19 UTC,
js,
autosave
Code: 08:19:51 UTC,
js,
autosave
Code: 08:20:28 UTC,
js,
autosave
Code: 08:20:42 UTC,
js,
autosave
Code: 08:22:02 UTC,
js,
autosave
Code: 08:22:22 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
Object.keys(classifiedNums).find(num => {
num
})
}
Code: 08:22:36 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
Object.keys(classifiedNums).find(num => {
classifiedNums[num]
})
}
Code: 08:22:51 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
Object.keys(classifiedNums).find(num =>
classifiedNums[num] === 1
)
}
Code: 08:23:02 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
return Object.keys(classifiedNums).find(num =>
classifiedNums[num] === 1
);
}
Analysis
expand all
Example tests
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'string' found
Code: 08:23:21 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
return IntegerObject.keys(classifiedNums).find(num =>
classifiedNums[num] === 1
);
}
Code: 08:23:39 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
return Object.keys(classifiedNums).find(num =>
classifiedNums[num] === 1
);
}
Code: 08:23:55 UTC,
js,
autosave
Code: 08:24:04 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
return Number.parseInt(Object.keys(classifiedNums).find(num =>
classifiedNums[num] === 1
));
}
Analysis
Code: 08:24:10 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
return Number.parseInt(Object.keys(classifiedNums).find(num =>
classifiedNums[num] === 1
));
}
Analysis
Code: 08:24:14 UTC,
js,
final,
score: 
55
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const classifiedNums = {};
for (num of A) {
if (!classifiedNums[num]) {
classifiedNums[num] = 0;
}
classifiedNums[num]++;
}
return Number.parseInt(Object.keys(classifiedNums).find(num =>
classifiedNums[num] === 1
));
}
Analysis summary
The following issues have been detected: runtime errors, timeout errors.
Analysis
Detected time complexity:
O(N**2)
expand all
Correctness tests
1.
0.052 s
OK
1.
0.052 s
OK
1.
0.052 s
OK
1.
0.060 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, non-integer number found
1.
0.056 s
OK
expand all
Performance tests
1.
0.060 s
OK
1.
0.224 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, non-integer number found
big1
big random test n=999,999, multiple repetitions
big random test n=999,999, multiple repetitions
✘
TIMEOUT ERROR
running time: 1.072 sec., time limit: 0.880 sec.
running time: 1.072 sec., time limit: 0.880 sec.
1.
1.072 s
TIMEOUT ERROR,
running time: 1.072 sec., time limit: 0.880 sec.
stderr:
Invalid result type, integer expected, non-integer number found
1.
1.716 s
TIMEOUT ERROR,
running time: 1.716 sec., time limit: 1.008 sec.