I'm currently tackling a challenge on CodeChefs:
My task is to find the smallest missing positive integer in an unsorted list of numbers. Here's the code snippet I've implemented:
var firstMissingPositive = function(nums) {
nums.sort();
let x = 1;
for (let num in nums) {
if (nums[num] <= 0) continue;
else if (nums[num] != x) break;
else x++;
}
return x;
};
Although this code successfully solves most test cases, it fails to pass a seemingly simple scenario -
nums = [1,2,3,4,5,6,7,8,9,20];
Instead of returning the expected output of 10, it gives me 3.
While I'm not expecting a direct solution, I'm curious as to why my loop breaks at 3 after handling 1 and 2 correctly. Any insights would be greatly appreciated!