Tasks Details
easy
1.
Distinct
Compute number of distinct values in an array.
Task Score
100%
Correctness
100%
Performance
100%
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C#
Time spent on task 17 minutes
Notes
not defined yet
Task timeline
Code: 14:06:38 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.Distinct.ToList().Count;
return unique;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(12,24): error CS1061: Type `int[]' does not contain a definition for `Distinct' and no extension method `Distinct' of type `int[]' could be found. Are you missing `System.Linq' using directive? /opt/codility-mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
Code: 14:10:15 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>().Distinct.ToList().Count;
return unique;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(13,38): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
Code: 14:11:17 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result= unique.Distinct.ToList();
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(14,28): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
Code: 14:11:55 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result= unique.Distinct.ToList<int>;
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(14,28): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
Code: 14:13:25 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result = unique.Distinct.ToList();
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(14,29): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
Code: 14:16:03 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
List<int> result = unique.Distinct.ToList();
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 2 error(s), 0 warnings Solution.cs(14,9): error CS0246: The type or namespace name `List' could not be found. Are you missing `System.Collections.Generic' using directive? Solution.cs(16,16): error CS0841: A local variable `result' cannot be used before it is declared
Code: 14:16:33 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
using System.Collection.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
List<int> result = unique.Distinct.ToList();
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(3,14): error CS0234: The type or namespace name `Collection' does not exist in the namespace `System'. Are you missing an assembly reference?
Code: 14:16:55 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
List<int> result = unique.Distinct.ToList();
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(15,35): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
Code: 14:17:18 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result = unique.Distinct.ToList();
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(15,29): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
Code: 14:17:44 UTC,
cs,
verify,
result: Failed
using System;
using System.Linq;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result = A.Distinct.ToArray();
return result.Count;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(15,24): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
Code: 14:19:22 UTC,
cs,
verify,
result: Passed
using System;
using System.Linq;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result = A.Distinct().ToList();
return result.Count;
}
}
Analysis
Code: 14:19:28 UTC,
cs,
verify,
result: Passed
using System;
using System.Linq;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result = A.Distinct().ToList();
return result.Count;
}
}
Analysis
Code: 14:19:31 UTC,
cs,
final,
score: 
100
using System;
using System.Linq;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var unique = A.ToList<int>();
var result = A.Distinct().ToList();
return result.Count;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N*log(N)) or O(N)
expand all
Correctness tests
1.
0.069 s
OK
1.
0.070 s
OK
2.
0.070 s
OK
1.
0.070 s
OK
1.
0.069 s
OK
1.
0.070 s
OK
1.
0.070 s
OK
1.
0.070 s
OK
1.
0.072 s
OK
1.
0.071 s
OK