Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
100%
Correctness
100%
Performance
100%
This is a demo task.
Write a function:
function solution(A);
that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..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 JavaScript
Time spent on task 8 minutes
Notes
not defined yet
Code: 05:30:17 UTC,
js,
verify,
result: Passed
function solution(A) {
var min = 1;
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0){
onlyPositiveInt[A[i]] = onlyPositiveInt[A[i]]? onlyPositiveInt[A[i]] + 1 : 1;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
}
Analysis
Code: 05:32:10 UTC,
js,
verify,
result: Passed
function solution(A) {
var min = 1;
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0){
onlyPositiveInt[A[i]] = onlyPositiveInt[A[i]]? onlyPositiveInt[A[i]] + 1 : 1;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
return
}
User test case 1:
[1, 3, 4, 1, 2, 6]
User test case 2:
[6, 3, 4, 1, 2, 1]
Analysis
Code: 05:33:01 UTC,
js,
verify,
result: Passed
function solution(A) {
var min = 1;
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0){
onlyPositiveInt[A[i]] = true;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
return
}
User test case 1:
[1, 3, 4, 1, 2, 6]
User test case 2:
[6, 3, 4, 1, 2, 1]
Analysis
Code: 05:34:26 UTC,
js,
verify,
result: Passed
function solution(A) {
var min = 1;
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0 && A[i] <= A.length){
onlyPositiveInt[A[i]] = true;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
}
User test case 1:
[1, 3, 4, 1, 2, 6]
User test case 2:
[6, 3, 4, 1, 2, 1]
Analysis
Code: 05:35:48 UTC,
js,
verify,
result: Passed
function solution(A) {
var min = 1;
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0 ){
onlyPositiveInt[A[i]] = true;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
}
User test case 1:
[-1, -3, 4, -1, -2, -6]
User test case 2:
[6, 3, 4, 1, 2, 1]
Analysis
Code: 05:36:16 UTC,
js,
verify,
result: Passed
function solution(A) {
var min = 1;
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0 ){
onlyPositiveInt[A[i]] = true;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
}
User test case 1:
[-1, -3, 4, -1, -2, -6]
User test case 2:
[6, 3, 4, 1, 2, 1]
User test case 3:
[-1, -3, -4, -1, -2, -6]
Analysis
expand all
User tests
1.
0.072 s
OK
function result: 1
function result: 1
1.
0.072 s
OK
function result: 5
function result: 5
1.
0.071 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
Code: 05:36:43 UTC,
js,
verify,
result: Passed
function solution(A) {
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0 ){
onlyPositiveInt[A[i]] = true;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
return 1
}
User test case 1:
[-1, -3, 4, -1, -2, -6]
User test case 2:
[6, 3, 4, 1, 2, 1]
User test case 3:
[-1, -3, -4, -1, -2, -6]
Analysis
expand all
User tests
1.
0.071 s
OK
function result: 1
function result: 1
1.
0.069 s
OK
function result: 5
function result: 5
1.
0.069 s
OK
function result: 1
function result: 1
Code: 05:37:41 UTC,
js,
verify,
result: Passed
function solution(A) {
var onlyPositiveInt = [];
for (var i =0; i < A.length; i++){
if (A[i] > 0 ){
onlyPositiveInt[A[i]] = true;
}
}
for (i = 1; i <= onlyPositiveInt.length; i++){
if (!onlyPositiveInt[i]){
return i;
}
}
return 1;
}
User test case 1:
[-1, -3, 4, -1, -2, -6]
User test case 2:
[6, 3, 4, 1, 2, 1]
User test case 3:
[-1, -3, -4, -1, -2, -6]
Analysis
expand all
User tests
1.
0.070 s
OK
function result: 1
function result: 1
1.
0.069 s
OK
function result: 5
function result: 5
1.
0.070 s
OK
function result: 1
function result: 1
Code: 05:37:46 UTC,
js,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.092 s
OK
2.
0.093 s
OK
3.
0.092 s
OK
1.
0.090 s
OK
2.
0.088 s
OK
1.
0.088 s
OK
2.
0.088 s
OK
1.
0.092 s
OK
1.
0.088 s
OK
expand all
Performance tests
1.
0.083 s
OK
2.
0.083 s
OK
3.
0.086 s
OK
1.
0.145 s
OK
1.
0.144 s
OK
2.
0.144 s
OK
1.
0.131 s
OK