Tasks Details
easy
1.
Distinct
Compute number of distinct values in an array.
Task Score
25%
Correctness
33%
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used PHP
Total time used 2 minutes
Effective time used 2 minutes
Notes
not defined yet
Task timeline
Code: 02:48:03 UTC,
java,
autosave
Code: 02:48:31 UTC,
php,
autosave
function solution($A) {
$unique = array_unique($A);
$aligned = array_values($unique);
return count($aligned);
}
function solution($A) {
3 $count = 0;
4 $array = array_fill(0, count($A), 0);
5
6 // 配列の要素に該当する場所に1をセットする
7 foreach($A as $value) {
8 if(! $array[$value]) {
9 $array[$value] = 1;
10 }
11 }
12 // 1がセットされている箇所を数える
13 foreach($array as $value) {
14 if($array[$value]) {
15 $count++;
16 }
17 }
18
19 return $count;
20
21 }
Code: 02:49:13 UTC,
php,
autosave
function solution($A) {
$count = 0;
$array = array_fill(0, count($A), 0);
// 配列の要素に該当する場所に1をセットする
foreach($A as $value) {
if(! $array[$value]) {
$array[$value] = 1;
}
}
// 1がセットされている箇所を数える
foreach($array as $value) {
if($array[$value]) {
$count++;
}
}
return $count;
}
Code: 02:49:14 UTC,
php,
verify,
result: Passed
function solution($A) {
$count = 0;
$array = array_fill(0, count($A), 0);
// 配列の要素に該当する場所に1をセットする
foreach($A as $value) {
if(! $array[$value]) {
$array[$value] = 1;
}
}
// 1がセットされている箇所を数える
foreach($array as $value) {
if($array[$value]) {
$count++;
}
}
return $count;
}
Analysis
Code: 02:49:18 UTC,
php,
verify,
result: Passed
function solution($A) {
$count = 0;
$array = array_fill(0, count($A), 0);
// 配列の要素に該当する場所に1をセットする
foreach($A as $value) {
if(! $array[$value]) {
$array[$value] = 1;
}
}
// 1がセットされている箇所を数える
foreach($array as $value) {
if($array[$value]) {
$count++;
}
}
return $count;
}
Analysis
Code: 02:49:20 UTC,
php,
final,
score: 
25
function solution($A) {
$count = 0;
$array = array_fill(0, count($A), 0);
// 配列の要素に該当する場所に1をセットする
foreach($A as $value) {
if(! $array[$value]) {
$array[$value] = 1;
}
}
// 1がセットされている箇所を数える
foreach($array as $value) {
if($array[$value]) {
$count++;
}
}
return $count;
}
Analysis summary
The following issues have been detected: wrong answers, runtime errors.
For example, for the input [0] the solution returned a wrong answer (got 0 expected 1).
Analysis
expand all
Correctness tests
1.
0.004 s
OK
1.
0.004 s
OK
stderr:
PHP Notice: Undefined offset: 1 in /tmp/exec.php on line 9 Notice: Undefined offset: 1 in /tmp/exec.php on line 9
2.
0.004 s
WRONG ANSWER,
got 0 expected 1
stderr:
PHP Notice: Undefined offset: 1 in /tmp/exec.php on line 15 Notice: Undefined offset: 1 in /tmp/exec.php on line 15
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
WRONG ANSWER,
got 0 expected 3
stderr:
PHP Notice: Undefined offset: -1 in /tmp/exec.php on line 9 Notice: Undefined offset: -1 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: -2 in /tmp/exec.php on line 9 Notice: Undefined offset: -2 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: -3 in /tmp/exec.php on line 9 Notice: Undefined offset: -3 in /tmp/exec.php on line 9
1.
0.004 s
WRONG ANSWER,
got 0 expected 3
stderr:
PHP Notice: Undefined offset: 65536 in /tmp/exec.php on line 9 Notice: Undefined offset: 65536 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 131072 in /tmp/exec.php on line 9 Notice: Undefined offset: 131072 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 196608 in /tmp/exec.php on line 9 Notice: Undefined offset: 196608 in /tmp/exec.php on line 9
1.
0.004 s
WRONG ANSWER,
got 0 expected 96
stderr:
PHP Notice: Undefined offset: 587 in /tmp/exec.php on line 9 Notice: Undefined offset: 587 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 362 in /tmp/exec.php on line 9 Notice: Undefined offset: 362 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 651 in /tmp/exec.php on line 9 Notice: Undefined offset: 651 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 951 in /tmp/exec.php on line 9 Notice: Undefined offset: 951 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 593 in /tmp/exec.php on line 9 Notice: Undefined offset: 593 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 569 in /tmp/exec.php on line 9 Notice: Undefined offset: 569 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 746 in /tmp/exec.php on line 9 Notice: Undefined offset: 746 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 628 in /tmp/exec.php on line 9 Notice: Undefined offset: 628 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 620 in /tmp/exec.php on line 9 Notice: Undefined offset: 620 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 253 in /tmp/exec.php on line 9 Notice: Undefined offset: 253 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 151 in /tmp/exec.php on line 9 Notice: Undefined offset: 151 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 642 in /tmp/exec.php on line 9 Notice: Undefined offset: 642 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 929 in /tmp/exec.php on line 9 Notice: Undefined offset: 929 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 436 in /tmp/exec.php on line 9 Notice: Undefined offset: 436 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 661 in /tmp/exec.php on line 9 Notice: Undefined offset: 661 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 353 in /tmp/exec.php on line 9 Notice: Undefined offset: 353 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 376 in /tmp/exec.php on line 9 Notice: Undefined offset: 376 in /tmp/exec.php on line 9 PHP Notice: Undefined of
1.
0.008 s
WRONG ANSWER,
got 342 expected 179
stderr:
PHP Notice: Undefined offset: 728 in /tmp/exec.php on line 9 Notice: Undefined offset: 728 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 500 in /tmp/exec.php on line 9 Notice: Undefined offset: 500 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 778 in /tmp/exec.php on line 9 Notice: Undefined offset: 778 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 564 in /tmp/exec.php on line 9 Notice: Undefined offset: 564 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 881 in /tmp/exec.php on line 9 Notice: Undefined offset: 881 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 885 in /tmp/exec.php on line 9 Notice: Undefined offset: 885 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 598 in /tmp/exec.php on line 9 Notice: Undefined offset: 598 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 267 in /tmp/exec.php on line 9 Notice: Undefined offset: 267 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 513 in /tmp/exec.php on line 9 Notice: Undefined offset: 513 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 994 in /tmp/exec.php on line 9 Notice: Undefined offset: 994 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 774 in /tmp/exec.php on line 9 Notice: Undefined offset: 774 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 878 in /tmp/exec.php on line 9 Notice: Undefined offset: 878 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 397 in /tmp/exec.php on line 9 Notice: Undefined offset: 397 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 366 in /tmp/exec.php on line 9 Notice: Undefined offset: 366 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 515 in /tmp/exec.php on line 9 Notice: Undefined offset: 515 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 574 in /tmp/exec.php on line 9 Notice: Undefined offset: 574 in /tmp/exec.php on line 9 PHP Notice: Undefined offset: 539 in /tmp/exec.php on line 9 Notice: Undefined offset: 539 in /tmp/exec.php on line 9 PHP Notice: Undefined of
1.
0.004 s
WRONG ANSWER,
got 200 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
✘
RUNTIME ERROR
tested program terminated unexpectedly
tested program terminated unexpectedly
1.
0.032 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
PHP Notice: Undefined offset: 86245 in func.php on line 7 Notice: Undefined offset: 86245 in func.php on line 7 PHP Notice: Undefined offset: 41569 in func.php on line 7 Notice: Undefined offset: 41569 in func.php on line 7 PHP Notice: Undefined offset: 39026 in func.php on line 7 Notice: Undefined offset: 39026 in func.php on line 7 PHP Notice: Undefined offset: 32099 in func.php on line 7 Notice: Undefined offset: 32099 in func.php on line 7 PHP Notice: Undefined offset: 18491 in func.php on line 7 Notice: Undefined offset: 18491 in func.php on line 7 PHP Notice: Undefined offset: 49365 in func.php on line 7 Notice: Undefined offset: 49365 in func.php on line 7 PHP Notice: Undefined offset: 81368 in func.php on line 7 Notice: Undefined offset: 81368 in func.php on line 7 PHP Notice: Undefined offset: 15738 in func.php on line 7 Notice: Undefined offset: 15738 in func.php on line 7 PHP Notice: Undefined offset: 57379 in func.php on line 7 Notice: Undefined offset: 57379 in func.php on line 7 PHP Notice: Undefined offset: 67192 in func.php on line 7 Notice: Undefined offset: 67192 in func.php on line 7 PHP Notice: Undefined offset: 11990 in func.php on line 7 Notice: Undefined offset: 11990 in func.php on line 7 PHP Notice: Undefined offset: 12677 in func.php on line 7 Notice: Undefined offset: 12677 in func.php on line 7 PHP Notice: Undefined offset: 14207 in func.php on line 7 Notice: Undefined offset: 14207 in func.php on line 7 PHP Notice: Undefined offset: 33689 in func.php on line 7 Notice: Undefined offset: 33689 in func.php on line 7 PHP Notice: Undefined offset: 17144 in func.php on line 7 Notice: Undefined offset: 17144 in func.php on line 7 PHP Notice: Undefined offset: 23772 in func.php on line 7 Notice: Undefined offset: 23772 in func.php on line 7 PHP Notice: Undefined offset: 68855 in func.php on line 7 Notice: Undefine
large_random1
chaotic sequence of values from [-1M..1M], length=100K
chaotic sequence of values from [-1M..1M], length=100K
✘
RUNTIME ERROR
tested program terminated unexpectedly
tested program terminated unexpectedly
1.
0.056 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
PHP Notice: Undefined offset: 278854 in func.php on line 7 Notice: Undefined offset: 278854 in func.php on line 7 PHP Notice: Undefined offset: -949979 in func.php on line 7 Notice: Undefined offset: -949979 in func.php on line 7 PHP Notice: Undefined offset: -449942 in func.php on line 7 Notice: Undefined offset: -449942 in func.php on line 7 PHP Notice: Undefined offset: -553579 in func.php on line 7 Notice: Undefined offset: -553579 in func.php on line 7 PHP Notice: Undefined offset: 472943 in func.php on line 7 Notice: Undefined offset: 472943 in func.php on line 7 PHP Notice: Undefined offset: 353399 in func.php on line 7 Notice: Undefined offset: 353399 in func.php on line 7 PHP Notice: Undefined offset: 784360 in func.php on line 7 Notice: Undefined offset: 784360 in func.php on line 7 PHP Notice: Undefined offset: -826123 in func.php on line 7 Notice: Undefined offset: -826123 in func.php on line 7 PHP Notice: Undefined offset: -156156 in func.php on line 7 Notice: Undefined offset: -156156 in func.php on line 7 PHP Notice: Undefined offset: -940406 in func.php on line 7 Notice: Undefined offset: -940406 in func.php on line 7 PHP Notice: Undefined offset: -562724 in func.php on line 7 Notice: Undefined offset: -562724 in func.php on line 7 PHP Notice: Undefined offset: -946929 in func.php on line 7 Notice: Undefined offset: -946929 in func.php on line 7 PHP Notice: Undefined offset: -602325 in func.php on line 7 Notice: Undefined offset: -602325 in func.php on line 7 PHP Notice: Undefined offset: 299769 in func.php on line 7 Notice: Undefined offset: 299769 in func.php on line 7 PHP Notice: Undefined offset: -559119 in func.php on line 7 Notice: Undefined offset: -559119 in func.php on line 7 PHP Notice: Undefined offset: 178531 in func.php on line 7 Notice: Undefined offset: 178531 in func.php on line 7 PHP Notice: Undefined offse
large_random2
another chaotic sequence of values from [-1M..1M], length=100K
another chaotic sequence of values from [-1M..1M], length=100K
✘
RUNTIME ERROR
tested program terminated unexpectedly
tested program terminated unexpectedly
1.
0.056 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
PHP Notice: Undefined offset: -182928 in func.php on line 7 Notice: Undefined offset: -182928 in func.php on line 7 PHP Notice: Undefined offset: 724074 in func.php on line 7 Notice: Undefined offset: 724074 in func.php on line 7 PHP Notice: Undefined offset: -646669 in func.php on line 7 Notice: Undefined offset: -646669 in func.php on line 7 PHP Notice: Undefined offset: -549681 in func.php on line 7 Notice: Undefined offset: -549681 in func.php on line 7 PHP Notice: Undefined offset: -941806 in func.php on line 7 Notice: Undefined offset: -941806 in func.php on line 7 PHP Notice: Undefined offset: -766699 in func.php on line 7 Notice: Undefined offset: -766699 in func.php on line 7 PHP Notice: Undefined offset: -982389 in func.php on line 7 Notice: Undefined offset: -982389 in func.php on line 7 PHP Notice: Undefined offset: -686620 in func.php on line 7 Notice: Undefined offset: -686620 in func.php on line 7 PHP Notice: Undefined offset: 978986 in func.php on line 7 Notice: Undefined offset: 978986 in func.php on line 7 PHP Notice: Undefined offset: 387021 in func.php on line 7 Notice: Undefined offset: 387021 in func.php on line 7 PHP Notice: Undefined offset: -243147 in func.php on line 7 Notice: Undefined offset: -243147 in func.php on line 7 PHP Notice: Undefined offset: -189905 in func.php on line 7 Notice: Undefined offset: -189905 in func.php on line 7 PHP Notice: Undefined offset: 382619 in func.php on line 7 Notice: Undefined offset: 382619 in func.php on line 7 PHP Notice: Undefined offset: -347760 in func.php on line 7 Notice: Undefined offset: -347760 in func.php on line 7 PHP Notice: Undefined offset: -852894 in func.php on line 7 Notice: Undefined offset: -852894 in func.php on line 7 PHP Notice: Undefined offset: 683046 in func.php on line 7 Notice: Undefined offset: 683046 in func.php on line 7 PHP Notice: Undefined off