Tasks Details
easy
1.
Distinct
Compute number of distinct values in an array.
Task Score
75%
Correctness
100%
Performance
0%
Write a function
class Solution { public int solution(int[] 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 C#
Time spent on task 15 minutes
Notes
not defined yet
Code: 09:36:35 UTC,
cs,
verify,
result: Failed
using System;
class Solution {
public int solution(int[] A) {
int cnt = 0;
List<int> list = new List<int>();
for(int i = 0; i < A.Length; i++)
{
for(int j = 0; j < list.Count; j++)
{
if(A[i] == list[j])
{
break;
}
else
{
list.Add(A[i]);
}
}
}
return list.Count;
}
}
Analysis
Compile error
Compilation failed: 5 error(s), 0 warnings Solution.cs(8,9): error CS0246: The type or namespace name `List' could not be found. Are you missing `System.Collections.Generic' using directive? Solution.cs(12,32): error CS0841: A local variable `list' cannot be used before it is declared Solution.cs(14,28): error CS0841: A local variable `list' cannot be used before it is declared Solution.cs(20,21): error CS0841: A local variable `list' cannot be used before it is declared Solution.cs(25,16): error CS0841: A local variable `list' cannot be used before it is declared
Code: 09:36:51 UTC,
cs,
verify,
result: Failed
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
int cnt = 0;
List<int> list = new List<int>();
for(int i = 0; i < A.Length; i++)
{
for(int j = 0; j < list.Count; j++)
{
if(A[i] == list[j])
{
break;
}
else
{
list.Add(A[i]);
}
}
}
return list.Count;
}
}
Analysis
expand all
Example tests
1.
0.032 s
WRONG ANSWER,
got 0 expected 3
Code: 09:39:43 UTC,
cs,
verify,
result: Failed
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
List<int> list = new List<int>();
for(int i = 0; i < A.Length; i++)
{
bool isFound = false;
for(int j = 0; j < list.Count; j++)
{
if(A[i] == list[j])
{
isFound = true;
break;
}
}
if(isFound) list.Add(A[i]);
}
return list.Count;
}
}
Analysis
expand all
Example tests
1.
0.045 s
WRONG ANSWER,
got 0 expected 3
Code: 09:39:50 UTC,
cs,
verify,
result: Passed
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
List<int> list = new List<int>();
for(int i = 0; i < A.Length; i++)
{
bool isFound = false;
for(int j = 0; j < list.Count; j++)
{
if(A[i] == list[j])
{
isFound = true;
break;
}
}
if(!isFound) list.Add(A[i]);
}
return list.Count;
}
}
Analysis
Code: 09:40:19 UTC,
cs,
verify,
result: Passed
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
List<int> list = new List<int>();
for(int i = 0; i < A.Length; i++)
{
bool isFound = false;
for(int j = 0; j < list.Count; j++)
{
if(A[i] == list[j])
{
isFound = true;
break;
}
}
if(!isFound) list.Add(A[i]);
}
return list.Count;
}
}
Analysis
Code: 09:40:22 UTC,
cs,
final,
score: 
75
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
List<int> list = new List<int>();
for(int i = 0; i < A.Length; i++)
{
bool isFound = false;
for(int j = 0; j < list.Count; j++)
{
if(A[i] == list[j])
{
isFound = true;
break;
}
}
if(!isFound) list.Add(A[i]);
}
return list.Count;
}
}
Analysis summary
The following issues have been detected: timeout errors.
Analysis
Detected time complexity:
O(N**2)
expand all
Correctness tests
1.
0.053 s
OK
1.
0.055 s
OK
2.
0.055 s
OK
1.
0.055 s
OK
1.
0.056 s
OK
1.
0.055 s
OK
1.
0.055 s
OK
1.
0.056 s
OK
1.
0.053 s
OK
1.
0.053 s
OK
expand all
Performance tests
large1
chaotic sequence of values from [0..100K], length=10K
chaotic sequence of values from [0..100K], length=10K
✘
TIMEOUT ERROR
running time: 0.29 sec., time limit: 0.26 sec.
running time: 0.29 sec., time limit: 0.26 sec.
1.
0.287 s
TIMEOUT ERROR,
running time: 0.29 sec., time limit: 0.26 sec.
large_random1
chaotic sequence of values from [-1M..1M], length=100K
chaotic sequence of values from [-1M..1M], length=100K
✘
TIMEOUT ERROR
running time: >6.00 sec., time limit: 0.47 sec.
running time: >6.00 sec., time limit: 0.47 sec.
1.
6.000 s
TIMEOUT ERROR,
running time: >6.00 sec., time limit: 0.47 sec.
large_random2
another chaotic sequence of values from [-1M..1M], length=100K
another chaotic sequence of values from [-1M..1M], length=100K
✘
TIMEOUT ERROR
running time: >6.00 sec., time limit: 0.47 sec.
running time: >6.00 sec., time limit: 0.47 sec.
1.
6.000 s
TIMEOUT ERROR,
running time: >6.00 sec., time limit: 0.47 sec.