Tasks Details
easy
Find value that occurs in odd number of elements.
Task Score
55%
Correctness
80%
Performance
25%
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9
- the elements at indexes 0 and 2 have value 9,
- the elements at indexes 1 and 3 have value 3,
- the elements at indexes 4 and 6 have value 9,
- the element at index 5 has value 7 and is unpaired.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9the function should return 7, as explained in the example above.
Write an efficient algorithm for the following assumptions:
- N is an odd integer within the range [1..1,000,000];
- each element of array A is an integer within the range [1..1,000,000,000];
- all but one of the values in A occur an even number of times.
Copyright 2009–2024 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: 08:19:46 UTC,
c,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
public int solution(int[] A) {
int unique=0;
int count=A.length;
int uniquecount=0;
int[] chkarray=new int[count];
for(int i=0;i<count;i++){
chkarray[i]=A[i];
}
for(int i=0;i<count;i++){
uniquecount=0;
for(int j=0;j<count;j++){
if(A[i]==chkarray[j]){
uniquecount++;
}
if(uniquecount==1){
unique=A[i];
break;
}
}
}
return unique;
// write your code in Java SE 8
}
}
Analysis
Compile error
func.c: In function 'solution': func.c:5:6: error: 'public' undeclared (first use in this function) public int solution(int[] A) { ^ func.c:5:6: note: each undeclared identifier is reported only once for each function it appears in func.c:5:13: error: expected ';' before 'int' public int solution(int[] A) { ^ func.c:31:1: warning: control reaches end of non-void function [-Wreturn-type] } ^
Code: 08:20:03 UTC,
c,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
int solution(int[] A) {
int unique=0;
int count=A.length;
int uniquecount=0;
int[] chkarray=new int[count];
for(int i=0;i<count;i++){
chkarray[i]=A[i];
}
for(int i=0;i<count;i++){
uniquecount=0;
for(int j=0;j<count;j++){
if(A[i]==chkarray[j]){
uniquecount++;
}
if(uniquecount==1){
unique=A[i];
break;
}
}
}
return unique;
// write your code in Java SE 8
}
}
Analysis
Compile error
func.c: In function 'solution': func.c:5:25: error: expected ';', ',' or ')' before 'A' int solution(int[] A) { ^ func.c:31:1: warning: control reaches end of non-void function [-Wreturn-type] } ^
Code: 08:21:37 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int unique=0;
int count=A.length;
int uniquecount=0;
int[] chkarray=new int[count];
for(int i=0;i<count;i++){
chkarray[i]=A[i];
}
for(int i=0;i<count;i++){
uniquecount=0;
for(int j=0;j<count;j++){
if(A[i]==chkarray[j]){
uniquecount++;
}
if(uniquecount==1){
unique=A[i];
break;
}
}
}
return unique;
// write your code in Java SE 8
}
}
Analysis
expand all
Example tests
1.
1.480 s
WRONG ANSWER,
got 9 expected 7
Code: 08:22:17 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int unique=0;
int count=A.length;
int uniquecount=0;
int[] chkarray=new int[count];
for(int i=0;i<count;i++){
chkarray[i]=A[i];
}
for(int i=0;i<count;i++){
uniquecount=0;
for(int j=0;j<count;j++){
if(A[i]==chkarray[j]){
uniquecount++;
}
}
if(uniquecount==1){
unique=A[i];
break;
}
}
return unique;
// write your code in Java SE 8
}
}
Analysis
Code: 08:22:27 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int unique=0;
int count=A.length;
int uniquecount=0;
int[] chkarray=new int[count];
for(int i=0;i<count;i++){
chkarray[i]=A[i];
}
for(int i=0;i<count;i++){
uniquecount=0;
for(int j=0;j<count;j++){
if(A[i]==chkarray[j]){
uniquecount++;
}
}
if(uniquecount==1){
unique=A[i];
break;
}
}
return unique;
// write your code in Java SE 8
}
}
Analysis
Code: 08:22:32 UTC,
java,
final,
score: 
55
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int unique=0;
int count=A.length;
int uniquecount=0;
int[] chkarray=new int[count];
for(int i=0;i<count;i++){
chkarray[i]=A[i];
}
for(int i=0;i<count;i++){
uniquecount=0;
for(int j=0;j<count;j++){
if(A[i]==chkarray[j]){
uniquecount++;
}
}
if(uniquecount==1){
unique=A[i];
break;
}
}
return unique;
// write your code in Java SE 8
}
}
Analysis summary
The following issues have been detected: wrong answers, timeout errors.
Analysis
Detected time complexity:
O(N**2)
expand all
Correctness tests
1.
1.442 s
OK
1.
1.424 s
OK
1.
1.423 s
OK
1.
1.434 s
WRONG ANSWER,
got 0 expected 42
1.
1.427 s
OK
expand all
Performance tests
1.
1.461 s
OK
medium2
medium random test n=100,003
medium random test n=100,003
✘
TIMEOUT ERROR
running time: >9.00 sec., time limit: 3.78 sec.
running time: >9.00 sec., time limit: 3.78 sec.
1.
9.000 s
TIMEOUT ERROR,
running time: >9.00 sec., time limit: 3.78 sec.
big1
big random test n=999,999, multiple repetitions
big random test n=999,999, multiple repetitions
✘
TIMEOUT ERROR
running time: >16.00 sec., time limit: 10.71 sec.
running time: >16.00 sec., time limit: 10.71 sec.
1.
16.000 s
TIMEOUT ERROR,
running time: >16.00 sec., time limit: 10.71 sec.
1.
22.000 s
TIMEOUT ERROR,
running time: >22.00 sec., time limit: 16.12 sec.