I am aware that this exact problem has been asked around before. The purpose of my question is to figure out why my specific solution is not working. Codility does not allow us to see the full test cases they use, and apparently my code is failing some of the test cases. My code has worked for most of the test cases I've tried, but failing for their ones which I can't see.
Problem: Write a function 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.
Given A = [2, 2, 2], the function should return 1.
etc
My solution
function solution(A) {
const positivesOfA = A.filter(elem => elem > 0);
const limit = Math.max(...A);
let res = [];
if (positivesOfA.length == 0) {
return 1;
} else {
for (var i = 1; i <= limit; i++) {
if (!A.includes(i)) {
res.push(i);
}
}
return (res.length == 0 ? limit + 1 : Math.min(...res))
}
}
What is wrong this?
EDIT: having looked into it, the codility platform does test for efficiency and performance. However, it says that I still failed 1/4 test cases. So the test case is still an issue with, along with performance for which I got 0 marks.
return i;when you find the first one.