I have a list in python like this:
myList = [1,14,2,5,3,7,8,12]
How can I easily find the first unused value? (in this case '4')
1 is the smallest positive integer Was this answer helpful?
Solution idea and steps We sort the array. Suppose we are using heap sort which is an in-place O(nlogn) sorting algorithm. Now we run a loop and skip the negative numbers and zero present in the starting positions of the array. Starting from the first index, when we find X[i] < 1, we increment i by 1.
Easy to read, easy to understand, gets the job done:
def solution(A):
smallest = 1
unique = set(A)
for int in unique:
if int == smallest:
smallest += 1
return smallest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With