Find next available integer in sorted list

Find next available integer in sorted list

I have a custom function that finds a next available integer and creates a part number. The function currently takes a sorted list and loops through each item to find the missing number in sequence. On some part numbers I run into an issue where statement limitations exceed max because the list can have up to 9999 possible items. Is anyone aware of any creative ways to rewrite this function without a loop?

list.sort();
nextAvailable = 0;
for each  num in list
{
if(num != nextAvailable)
{
break;
}
nextAvailable = nextAvailable + 1;
}