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 4 minutes
Notes
not defined yet
Code: 04:22:40 UTC,
java,
autosave
Code: 04:22:49 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<A.length; i++){
if(!check[i]) return i;
}
return A.length+1;
}
Code: 04:22:49 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<A.length; i++){
if(!check[i]) return i;
}
return A.length+1;
}
Code: 04:22:58 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length).fill(false);
for(let a of A){
check[a] = true;
}
console.log(check)
for(let i = 1; i<A.length; i++){
if(!check[i]) return i;
}
return A.length+1;
}
Analysis
expand all
Example tests
1.
0.072 s
OK
stdout:
[ false, true, true, true, true, false, true ]
1.
0.072 s
OK
stdout:
[ false, true, true, true ]
1.
0.072 s
OK
stdout:
[ false, false, '-1': true, '-3': true ]
expand all
User tests
1.
0.072 s
OK
function result: 2
function result: 2
stdout:
[ false, <1 empty item>, true ]
User test case 1:
[2]
Code: 04:23:42 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length+).fill(false);
for(let a of A){
check[a] = true;
}
console.log(check)
for(let i = 1; i<A.length; i++){
if(!check[i]) return i;
}
return A.length+1;
}
Code: 04:23:43 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length+1).fill(false);
for(let a of A){
check[a] = true;
}
console.log(check)
for(let i = 1; i<A.length; i++){
if(!check[i]) return i;
}
return A.length+1;
}
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
[ false, true, true, true, true, false, true ]
1.
0.072 s
OK
stdout:
[ false, true, true, true ]
1.
0.072 s
OK
stdout:
[ false, false, false, '-1': true, '-3': true ]
expand all
User tests
1.
0.072 s
OK
function result: 2
function result: 2
stdout:
[ false, false, true ]
User test case 1:
[2]
Code: 04:24:10 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length+1).fill(false);
for(let a of A){
check[a] = true;
}
console.log(check)
for(let i = 1; i<A.length; i++){
if(check[i] !== true) return i;
}
return A.length+1;
}
Code: 04:24:10 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length+1).fill(false);
for(let a of A){
check[a] = true;
}
console.log(check)
for(let i = 1; i<A.length; i++){
if(check[i] !== true) return i;
}
return A.length+1;
}
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
[ false, true, true, true, true, false, true ]
1.
0.076 s
OK
stdout:
[ false, true, true, true ]
1.
0.072 s
OK
stdout:
[ false, false, false, '-1': true, '-3': true ]
expand all
User tests
1.
0.072 s
OK
function result: 2
function result: 2
stdout:
[ false, false, true ]
User test case 1:
[2]
Code: 04:24:24 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length+1).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<A.length; i++){
console.log(check[i])
if(check[i] !== true) return i;
}
return A.length+1;
}
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
true true true true false
1.
0.072 s
OK
stdout:
true true
1.
0.076 s
OK
stdout:
false
User test case 1:
[2]
Code: 04:24:38 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length+1).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<A.length+1; i++){
console.log(check[i])
if(check[i] !== true) return i;
}
return A.length+1;
}
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
true true true true false
1.
0.076 s
OK
stdout:
true true true
1.
0.072 s
OK
stdout:
false
User test case 1:
[2]
Code: 04:24:48 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const check = new Array(A.length+1).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<A.length+1; i++){
if(check[i] !== true) return i;
}
return A.length+1;
}
Code: 04:24:59 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = new Array(A.length+1).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<A.length+1; i++){
if(check[i] !== true) return i;
}
return A.length+1;
}
Code: 04:25:13 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = new Array(len).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<len; i++){
if(check[i] !== true) return i;
}
return A.length+1;
}
Code: 04:25:16 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = new Array(len).fill(false);
for(let a of A){
check[a] = true;
}
for(let i = 1; i<len; i++){
if(check[i] !== true) return i;
}
return len;
}
Code: 04:25:22 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = [];
for(let a of A){
check[a] = true;
}
for(let i = 1; i<len; i++){
if(check[i] !== true) return i;
}
return len;
}
Code: 04:25:56 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = [];
for(let a of A){
check[a] = true;
}
for(let i = 1; i<len; i++){
if(check[i]) return i;
}
return len;
}
Code: 04:25:58 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = [];
for(let a of A){
check[a] = true;
}
for(let i = 1; i<len; i++){
if(!check[i]) return i;
}
return len;
}
Code: 04:26:15 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = [];
for(let a of A){
check[a] = true;
}
for(let i = 1; i<len; i++){
if(!check[i]) return i;
}
return len;
}
Code: 04:26:17 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const len = A.length+1;
const check = [];
for(let a of A){
check[a] = true;
}
for(let i = 1; i<len; i++){
if(!check[i]) return i;
}
return len;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.072 s
OK
2.
0.104 s
OK
3.
0.072 s
OK
4.
0.068 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
expand all
Performance tests
1.
0.084 s
OK
2.
0.084 s
OK
3.
0.092 s
OK
1.
0.152 s
OK
1.
0.124 s
OK
2.
0.124 s
OK
1.
0.120 s
OK