Tasks Details
easy
1.
Distinct
Compute number of distinct values in an array.
Task Score
50%
Correctness
66%
Performance
0%
Write a function
function solution(A);
that, given an array A consisting of N integers, returns the number of distinct values in array A.
For example, given array A consisting of six elements such that:
A[0] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−1,000,000..1,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 6 minutes
Notes
not defined yet
Task timeline
Code: 21:30:25 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.096 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 1, 1, 2, 2, 3 ]
Code: 21:32:51 UTC,
js,
verify,
result: Failed
// you can use console.log for debugging purposes, i.e.
// console.log('this is a debug message');
function solution(A) {
A.sort(function(a,b){return a > b});
var count = 0;
if(A.length == 1) return 1;
for(var i=0; i<A.length-1;i++){
if(A[i] !== A[i++]){
count++;
}
}
return count;
}
Analysis
expand all
Example tests
1.
0.100 s
WRONG ANSWER,
got 0 expected 3
Code: 21:33:20 UTC,
js,
verify,
result: Failed
// you can use console.log for debugging purposes, i.e.
// console.log('this is a debug message');
function solution(A) {
A.sort(function(a,b){return a > b});
var count = 0;
if(A.length == 1) return 1;
for(var i=0; i<A.length-1;i++){
if(A[i] !== A[i+1]){
count++;
}
}
return count;
}
Analysis
expand all
Example tests
1.
0.088 s
WRONG ANSWER,
got 2 expected 3
Code: 21:33:45 UTC,
js,
verify,
result: Passed
// you can use console.log for debugging purposes, i.e.
// console.log('this is a debug message');
function solution(A) {
A.sort(function(a,b){return a > b});
var count = 1;
if(A.length == 0) return 0;
for(var i=0; i<A.length-1;i++){
if(A[i] !== A[i+1]){
count++;
}
}
return count;
}
Analysis
Code: 21:33:55 UTC,
js,
verify,
result: Passed
// you can use console.log for debugging purposes, i.e.
// console.log('this is a debug message');
function solution(A) {
A.sort(function(a,b){return a > b});
var count = 1;
if(A.length == 0) return 0;
for(var i=0; i<A.length-1;i++){
if(A[i] !== A[i+1]){
count++;
}
}
return count;
}
Analysis
Code: 21:34:00 UTC,
js,
final,
score: 
50
// you can use console.log for debugging purposes, i.e.
// console.log('this is a debug message');
function solution(A) {
A.sort(function(a,b){return a > b});
var count = 1;
if(A.length == 0) return 0;
for(var i=0; i<A.length-1;i++){
if(A[i] !== A[i+1]){
count++;
}
}
return count;
}
Analysis summary
The following issues have been detected: wrong answers.
Analysis
expand all
Correctness tests
1.
0.092 s
OK
1.
0.096 s
OK
1.
0.100 s
OK
1.
0.104 s
OK
1.
0.100 s
OK
1.
0.096 s
OK
1.
0.104 s
WRONG ANSWER,
got 99 expected 96
1.
0.100 s
WRONG ANSWER,
got 198 expected 179
1.
0.104 s
WRONG ANSWER,
got 83 expected 11
expand all
Performance tests
large1
chaotic sequence of values from [0..100K], length=10K
chaotic sequence of values from [0..100K], length=10K
✘
WRONG ANSWER
got 9997 expected 9522
got 9997 expected 9522
1.
0.144 s
WRONG ANSWER,
got 9997 expected 9522
large_random1
chaotic sequence of values from [-1M..1M], length=100K
chaotic sequence of values from [-1M..1M], length=100K
✘
WRONG ANSWER
got 100000 expected 97534
got 100000 expected 97534
1.
0.300 s
WRONG ANSWER,
got 100000 expected 97534
large_random2
another chaotic sequence of values from [-1M..1M], length=100K
another chaotic sequence of values from [-1M..1M], length=100K
✘
WRONG ANSWER
got 99997 expected 97527
got 99997 expected 97527
1.
0.332 s
WRONG ANSWER,
got 99997 expected 97527