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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 8 minutes
Notes
not defined yet
Task timeline
Code: 21:11:47 UTC,
java,
verify,
result: Failed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] != 0 ) {
++countDistincts;
}
if ( negatives[i] != 0 ) {
++countDistincts;
}
if ( countDistincts == length ) {
break; // no need to compare all elements
}
}
return countDistincts;
}
}
Analysis
expand all
Example tests
1.
1.312 s
WRONG ANSWER,
got 6 expected 3
Code: 21:14:53 UTC,
java,
verify,
result: Failed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] >= 1 ) {
++countDistincts;
}
if ( negatives[i] >= 1 ) {
++countDistincts;
}
}
return countDistincts - length;
}
}
Analysis
expand all
Example tests
1.
1.324 s
WRONG ANSWER,
got 0 expected 3
Code: 21:15:16 UTC,
java,
verify,
result: Failed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
if ( negatives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
}
return countDistincts - length;
}
}
Analysis
expand all
Example tests
1.
1.369 s
WRONG ANSWER,
got -3 expected 3
Code: 21:15:34 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
if ( negatives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
}
return length - countDistincts ;
}
}
Analysis
Code: 21:15:45 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
if ( negatives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
}
return length - countDistincts ;
}
}
User test case 1:
[0]
Analysis
Code: 21:16:08 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
if ( negatives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
}
return length - countDistincts ;
}
}
User test case 1:
[0]
User test case 2:
[-1, 0, 1, 1]
Analysis
expand all
User tests
1.
1.336 s
OK
function result: 0
function result: 0
1.
1.333 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at Solution.solution(Solution.java:20) at wrapper.run(wrapper.java:40) at wrapper.main(wrapper.java:29)
Code: 21:16:22 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
if ( negatives[i] >= 1 ) {
++countDistincts;
negatives[i] = 0;
}
}
return length - countDistincts ;
}
}
User test case 1:
[0]
User test case 2:
[-1, 0, 1, 1]
Analysis
expand all
User tests
1.
1.369 s
OK
function result: 0
function result: 0
1.
1.380 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at Solution.solution(Solution.java:20) at wrapper.run(wrapper.java:40) at wrapper.main(wrapper.java:29)
Code: 21:16:55 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
if ( negatives[i] >= 1 ) {
++countDistincts;
negatives[i * -1] = 0;
}
}
return length - countDistincts ;
}
}
User test case 1:
[0]
User test case 2:
[-1, 0, 1, 1]
Analysis
expand all
User tests
1.
1.332 s
OK
function result: 0
function result: 0
1.
1.339 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at Solution.solution(Solution.java:20) at wrapper.run(wrapper.java:40) at wrapper.main(wrapper.java:29)
Code: 21:17:30 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if (i >= 0 ) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
} else {
if ( negatives[i * -1] >= 1 ) {
++countDistincts;
negatives[i * -1] = 0;
}
}
}
return length - countDistincts ;
}
}
User test case 1:
[0]
User test case 2:
[-1, 0, 1, 1]
Analysis
Code: 21:17:46 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if (i >= 0 ) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
} else {
if ( negatives[i * -1] >= 1 ) {
++countDistincts;
negatives[i * -1] = 0;
}
}
}
return countDistincts ;
}
}
User test case 1:
[0]
User test case 2:
[-1, 0, 1, 1]
Analysis
Code: 21:18:26 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if (i >= 0 ) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
} else {
if ( negatives[i * -1] >= 1 ) {
++countDistincts;
negatives[i * -1] = 0;
}
}
}
return countDistincts ;
}
}
User test case 1:
[0]
User test case 2:
[-1, 0, 1, 1]
Analysis
Code: 21:18:35 UTC,
java,
verify,
result: Passed
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if (i >= 0 ) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
} else {
if ( negatives[i * -1] >= 1 ) {
++countDistincts;
negatives[i * -1] = 0;
}
}
}
return countDistincts ;
}
}
User test case 1:
[0]
User test case 2:
[-1, 0, 1, 1]
Analysis
Code: 21:18:43 UTC,
java,
final,
score: 
100
class Solution {
public int solution(int[] A) {
int length = A.length;
int inputLimit = 1000000;
int[] positives = new int[inputLimit];
int[] negatives = new int[inputLimit]; // should be length - 1 not counting zero
for (int element : A) {
if ( element >=0 ) {
++positives[element];
} else {
int abs = element * -1;
++negatives[abs];
}
}
int countDistincts = 0;
for (int i: A) {
if (i >= 0 ) {
if ( positives[i] >= 1 ) {
++countDistincts;
positives[i] = 0;
}
} else {
if ( negatives[i * -1] >= 1 ) {
++countDistincts;
negatives[i * -1] = 0;
}
}
}
return countDistincts ;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N*log(N))
expand all
Correctness tests
1.
1.354 s
OK
1.
1.363 s
OK
2.
1.183 s
OK
1.
1.335 s
OK
1.
1.353 s
OK
1.
1.351 s
OK
1.
1.354 s
OK
1.
1.344 s
OK
1.
1.350 s
OK
1.
1.351 s
OK