If you have a few years of experience in the Java ecosystem and you'd like to share that with the community, have a look at our **Contribution Guidelines**.

# Find the Smallest Missing Integer in an Array

Last modified: May 23, 2022

## 1. Overview

In this tutorial, we'll see different algorithms allowing us to find the smallest missing positive integer in an array.

First, we'll go through the explanation of the problem. After that, we'll see three different algorithms suiting our needs. Finally, we'll discuss their complexities.

## 2. Problem Explanation

First, let's explain what the goal of the algorithm is. We want to search for the smallest missing positive integer in an array of positive integers. **That is, in an array ofÂ x elements, find the smallest element between 0 andÂ x – 1 that is not in the array.** If the array contains them all, then the solution is

*x*, the array size.

For example, let's consider the following array:Â *[0, 1, 3, 5, 6]*. It hasÂ *5* elements. That means we're searching for the smallest integer between *0* and *4* that is not in this array. In this specific case, it's *2*.

Now, let's imagine another array:Â *[0, 1, 2, 3]*. As it hasÂ *4* elements, we're searching for an integer betweenÂ *0 *andÂ *3*. None is missing, thus the smallest integer that is not in the array isÂ *4*.

## 3. Sorted Array

Now, let's see how to find the smallest missing number in a sorted array. In a sorted array, the smallest missing integer would be the first index that doesn't hold itself as a value.

Let's consider the following sorted array: *[0, 1, 3, 4, 6, 7]*. Now, let's see which value matches which index:

```
Index: 0 1 2 3 4 5
Value: 0 1 3 4 6 7
```

As we can see, the value index doesn't hold integer *2*, thereforeÂ *2* is the smallest missing integer in the array.

How about implementing this algorithm in Java? Let's first create a classÂ *SmallestMissingPositiveInteger* with a methodÂ *searchInSortedArray()*:

```
public class SmallestMissingPositiveInteger {
public static int searchInSortedArray(int[] input) {
// ...
}
}
```

Now, we can iterate over the array and **search for the first index that doesn't contain itself as a value** and return it as the result:

```
for (int i = 0; i < input.length; i++) {
if (i != input[i]) {
return i;
}
}
```

Finally, **if we complete the loop without finding a missing element, we must return the next integer, which isÂ the array length**, as we start at index *0*:

`return input.length;`

Let's check that this all works as expected. Imagine an array of integers fromÂ *0* toÂ *5*, with the numberÂ *3* missing:

`int[] input = new int[] {0, 1, 2, 4, 5};`

Then, if we search for the first missing integer,Â *3* should be returned:

```
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
assertThat(result).isEqualTo(3);
```

But, if we search for a missing number in an array without any missing integer:

`int[] input = new int[] {0, 1, 2, 3, 4, 5};`

We'll find that the first missing integer is *6*, which is the length of the array:

```
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
assertThat(result).isEqualTo(input.length);
```

Next, we'll see how to handle unsorted arrays.

## 4. Unsorted Array

So, what about finding the smallest missing integer in an unsorted array? There are multiple solutions. The first one is to simply sort the array first and then reuse our previous algorithm. Another approach would be to use another array to flag the integers that are present and then traverse that array to find the first one missing.

### 4.1. Sorting the Array First

Let's start with the first solution and create a new *searchInUnsortedArraySortingFirst()* method.

**So, we'll be reusing our algorithm, but first, we need to sort our input array.** In order to do that, we'll make use of *Arrays.sort()*:

`Arrays.sort(input);`

That method sorts its input according to its natural order. For integers, that means from the smallest to the greatest one. There are more details about sorting algorithms in our article on sorting arrays in Java.

After that, we can call our algorithm with the now sorted input:

`return searchInSortedArray(input);`

That's it, we can now check that everything works as expected. Let's imagine the following array with unsorted integers and missing numbersÂ *1* andÂ *3*:

`int[] input = new int[] {4, 2, 0, 5};`

AsÂ *1* is the smallest missing integer, we expect it to be the result of calling our method:

```
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
assertThat(result).isEqualTo(1);
```

Now, let's try it on an array with no missing number:

```
int[] input = new int[] {4, 5, 1, 3, 0, 2};
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
assertThat(result).isEqualTo(input.length);
```

That's it, the algorithm returnsÂ *6*, that is the array length.

### 4.2. Using a Boolean Array

Another possibility is to use another array â€“ having the same length as the input array â€“ that holds *boolean* values telling if the integer matching an index has been found in the input array or not.

First, let's create a third method,Â *searchInUnsortedArrayBooleanArray()*.

After that, let's create the boolean array,Â *flags*, and **for each integer in the input array that matches an index of the boolean array, we set the corresponding value to true**:

```
boolean[] flags = new boolean[input.length];
for (int number : input) {
if (number < flags.length) {
flags[number] = true;
}
}
```

Now, ourÂ *flags* array holdsÂ *true* for each integer present in the input array, and *false* otherwise. Then, we can **iterate over theÂ flags array and return the first index holdingÂ false**. If none, we return the array length:

```
for (int i = 0; i < flags.length; i++) {
if (!flags[i]) {
return i;
}
}
return flags.length;
```

Again, let's try this algorithm with our examples. We'll first reuse the array missingÂ *1* andÂ *3*:

`int[] input = new int[] {4, 2, 0, 5};`

Then, when searching for the smallest missing integer with our new algorithm, the answer is stillÂ *1*:

```
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
assertThat(result).isEqualTo(1);
```

And for the complete array, the answer doesn't change either and is stillÂ *6*:

```
int[] input = new int[] {4, 5, 1, 3, 0, 2};
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
assertThat(result).isEqualTo(input.length);
```

## 5. Complexities

Now that we've covered the algorithms, let's talk about their complexities, using Big O notation.

### 5.1. Sorted Array

Let's start with the first algorithm, for which the input is already sorted. In this case, the worst-case scenario is not finding a missing integer and, therefore, traversing the entire array. This means **we have linear complexity**, which is notedÂ *O(n)*, consideringÂ *nÂ *is the length of our input.

### 5.2. Unsorted Array with Sorting Algorithm

Now, let's consider our second algorithm. In this case, the input array is not sorted, and we sort it before applying the first algorithm. Here, **the complexity will be the greatest between that of the sorting mechanism and that of the algorithm itself**.

As of Java 11, theÂ *Arrays.sort()* method uses a dual-pivot quick-sort algorithm to sort arrays. The complexity of this sorting algorithm is, in general,Â *O(n log(n))*, though it could degrade up toÂ *O(nÂ²)*. That means **the complexity of our algorithm will beÂ O(n log(n)) in general and can also degrade up to a quadratic complexity ofÂ O(nÂ²)**.

That's for time complexity, but let's not forget about space. Although the search algorithm doesn't take extra space, the sorting algorithm does. **Quick-sort algorithm takes up toÂ O(log(n)) space to execute.** That's something we may want to consider when choosing an algorithm for large arrays.

### 5.3. Unsorted Array with Boolean Array

Finally, let's see how our third and last algorithm performs. For this one, we don't sort the input array, which means **we don't suffer the complexity of sorting**. As a matter of fact, we only traverse two arrays, both of the same size. That means **our time complexity should beÂ O(2n), which is simplified to O(n)**. That's better than the previous algorithm.

But, when it comes to space complexity, we're creating a second array of the same size as the input. That means **we haveÂ O(n) space complexity**, which is worse than the previous algorithm.

Knowing all that, it's up to us to choose an algorithm that best suits our needs, depending on the conditions in which it'll be used.

## 6. Conclusion

In this article, we've looked at algorithms for finding the smallest missing positive integer in an array. We've seen how to achieve that in a sorted array, as well as in an unsorted array. We also discussed the time and space complexities of the different algorithms, allowing us to choose one wisely according to our needs.

As usual, the complete code examples shown in this article are available over on GitHub.