Collection Types

I have decided to write the function for returning the smallest positive integer that does not occur within an array.

I have used a list to store the array of numbers as this can store many different elements, I have a Boolean to loop the code until it has found a number that is not in the list, I have used this data type as the loop only needs to be one or the other which is what a Boolean is so that is why I have used it for the loop. Finally I have an integer called count to track what numbers the code has looped over as this is iterable and can count up in whole numbers.

The algorithm works by having an infinite loop until a number is not found within the array and then breaks out of the loop, the count variable adds 1 to itself when a number is found so that it can then check for the next number. For example, within my code snippet it will return the number 2 as it will enter the loop and then check if count which is 1 at this point is in any position within the numbers array as it is it will add 1 to count and loop again, this time when it enters the loop it will see that count now equals 2 and that is not in the numbers array so therefore will continue to the else block and print the current count and then break out of the loop.

Leave a Reply