If a number only appears exactly one time, it is pretty easy to tell in the following way:
Create a boolean array, boolArray
, of size of the given number; here it is 100.
Loop through the input numbers and set an element to true according the number value. For example, if 45 found, then set boolArray[45-1] = true
;
That will be an O(N) operation.
Then loop through boolArray
. If an element is staying false, then the index of element + 1 is the missing number. For example, if boolArray[44]
is false, we know number 45 is missing.
That is an O(n) operation. Space complexity is O(1).
So this solution can work to find any missing number from a given continuous number set.