# Find the first non-consecutive number in Java ## The challenge

Your task is to find the first element of an array that is not consecutive.

By not consecutive we mean not exactly 1 larger than the previous element of the array.

E.g. If we have an array `[1,2,3,4,6,7,8]` then `1` then `2` then `3` then `4` are all consecutive but `6` is not, so that’s the first non-consecutive number.

If the whole array is consecutive then return `null`.

The array will always have at least `2` elements1 and all elements will be numbers. The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too!

Can you write a solution that will return `null` for both `[]` and `[ x ]` though? (This is an empty array and one with a single number and is not tested for, but you can write your own example test. )

## The solution in Java code

In our solution, we loop through all elements of the array, except for the last element. Each time checking if the next element’s value is 1 more than the current element.

If it is not, then we return the next element’s value.

If all else fails, and we complete our loop, then we return `null`.

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```class FirstNonConsecutive {
static Integer find(final int[] array) {

for (int i=0; i<array.length-1; i++) {
if (array[i+1]!=array[i]+1)
return array[i+1];
}

return null;
}
}
```Code language: Java (java)```

We could also come to a similar solution by using the `IntStream` functionality, as follows:

``````import java.util.stream.*;

class FirstNonConsecutive {
static Integer find(final int[] array) {
return IntStream.range(1, array.length).filter(i -> array[i-1] != array[i]-1)
.mapToObj(x -> new Integer(array[x])).findFirst().orElse(null);
}
}
```Code language: Java (java)```

## Test cases to validate our Java code

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;

public final class FirstNonConsecutiveTest {
@Test public void basicTests() {
assertEquals(Integer.valueOf(6), FirstNonConsecutive.find(new int[]{1, 2, 3, 4, 6, 7, 8}));
assertEquals(null, FirstNonConsecutive.find(new int[]{1, 2, 3, 4, 5, 6, 7, 8}));
assertEquals(Integer.valueOf(6), FirstNonConsecutive.find(new int[]{4, 6, 7, 8, 9, 11}));
assertEquals(Integer.valueOf(11), FirstNonConsecutive.find(new int[]{4, 5, 6, 7, 8, 9, 11}));
assertEquals(null, FirstNonConsecutive.find(new int[]{31, 32}));
assertEquals(Integer.valueOf(0), FirstNonConsecutive.find(new int[]{-3, -2, 0, 1}));
assertEquals(Integer.valueOf(-1), FirstNonConsecutive.find(new int[]{-5, -4, -3, -1}));
}
}
```Code language: Java (java)```

## Extended test cases

``````import java.util.Arrays;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public final class FirstNonConsecutiveTest {
@Test public void basicTests() {
assertEquals(Integer.valueOf(6), FirstNonConsecutive.find(new int[]{1, 2, 3, 4, 6, 7, 8}));
assertEquals(null, FirstNonConsecutive.find(new int[]{1, 2, 3, 4, 5, 6, 7, 8}));
assertEquals(Integer.valueOf(6), FirstNonConsecutive.find(new int[]{4, 6, 7, 8, 9, 11}));
assertEquals(Integer.valueOf(11), FirstNonConsecutive.find(new int[]{4, 5, 6, 7, 8, 9, 11}));
assertEquals(null, FirstNonConsecutive.find(new int[]{31, 32}));
assertEquals(Integer.valueOf(0), FirstNonConsecutive.find(new int[]{-3, -2, 0, 1}));
assertEquals(Integer.valueOf(-1), FirstNonConsecutive.find(new int[]{-5, -4, -3, -1}));
}

@Test public void randomTests() {
Random random = new Random();
for (int i = 0; i < 20; i++) {
int length = random.nextInt(15) + 1;
int[] array = new int[length];
int start = random.nextInt(10000) - (10000/4); //25% should start less than zero
for (int j=0; j<length; j++) {
array[j] = start;
start++;
if (random.nextInt(100) > 90) start++;
}
assertEquals("For input \"" + Arrays.toString(array) + '"', kata(array), FirstNonConsecutive.find(array));
}
}

private static Integer kata(final int[] array) {
for (int i = 1; i < array.length; i++) {
if (array[i] != array[i - 1] + 1) {
return array[i];
}
}
return null;
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of 