function solution(A) {
for (i = 1; i < 1000000; i++) {
if(!A.includes(i)) return i;
}
}
Be consistent with your spacing. You use a space after for
but not for if
. The space helps separate language constructs from function calls.
For languages that use braces, always brace your one-liners (explicitly establishes the loop boundary) and prefer to have them on a separate line for readability, maintenance, and debugging (breakpoints!).
What happens if len(A) >= 1,000,000
? Use the length of the array instead of an arbitrary value. See below.
You can simplify this problem by filtering/partitioning any non-positive value from the array. Once you have a filtered array of positive integers, you can use the filtered length to determine the upper-bound of the lowest positive integer. For a distinct sequence of integers \$D = [1, 2, 3, ..., n]\$, the lowest positive integer is guaranteed to be \$n+1\$. If you remove any value from \$D\$ and replace it with any other value (or simply remove it), then the lowest positive integer of \$D\$ is in the range \$[1, n]\$. To find it, we can simply track integers in a boolean table, upto \$n\$, marking the ones witnessed. A linear search of the boolean array for the first unmarked entry will give us a zero-based index of the lowest positive integer missing. Add one to make it one-based once again. Filtering, marking witnesses, and searching are all linear operations.
Note - Since you know the upper-bound, you can narrow your range further by making a second filter pass which removes any elements larger than the array length. Would help with data locality if you have smallish arrays loaded with largish values.
While using a boolean array does meet your space complexity requirement, a constant space solution does exist. Remember that every element in your filtered array is positive, so we can repurpose the sign bit of each value as a signal that we've witnessed a value of the sequence. We can use the indices of the filtered array the same way we did the boolean array above. Rather than search for the first element marked false (unwitnessed), we search for the first value that is still positive.
solution(A)
Filter non-positive values from A
Filter values larger than min(N-1, 999999) from A
For each int in A that wasn't filtered out
Let a zero-based index be the absolute value of the int - 1
For each index upto min(N-1, 999999)
if A[index] is positive, return the index + 1 (to one-based)
otherwise return min(N, 100000)
So an array \$A = [ 1, 2, 3, 5, 6]\$, would have the following transformations:
abs(A[0]) = 1, to_0idx = 0, A[0] = 1, make_negative(A[0]), A = [-1, 2, 3, 5, 6]
abs(A[1]) = 2, to_0idx = 1, A[1] = 2, make_negative(A[1]), A = [-1, -2, 3, 5, 6]
abs(A[2]) = 3, to_0idx = 2, A[2] = 3, make_negative(A[2]), A = [-1, -2, -3, 5, 6]
abs(A[3]) = 5, to_0idx = 4, A[4] = 6, make_negative(A[4]), A = [-1, -2, -3, 5, -6]
A linear search for the first positive value returns an index of 3. Converting back to a one-based index results in \$solution(A) = 3 + 1 = 4\$