Finding the first non-consecutive number in an array in JavaScript

In JavaScript, finding the first non-consecutive number in an array means identifying the first element that is not exactly one more than its previous element. This is useful for detecting gaps in sequential data.

Problem Statement

We need to write a JavaScript function that takes an array of numbers and returns the first element that breaks the consecutive sequence. The function should return the element that is not the natural successor (+1) of its previous element.

Algorithm

The approach is to iterate through the array and compare each element with its previous element. When we find an element that is not exactly one more than the previous element, we return that element.

Example

const arr = [1, 2, 3, 4, 6, 7, 8];

const findFirstNonConsecutive = (arr = []) => {
    for(let i = 0; i < arr.length - 1; i++){
        const el = arr[i];
        const next = arr[i + 1];
        if(next - el !== 1){
            return next;
        }
    }
    return null;
};

console.log(findFirstNonConsecutive(arr));
6

How It Works

In the example above, the array [1, 2, 3, 4, 6, 7, 8] has a gap between 4 and 6. The function iterates through pairs of consecutive elements:

  • 1 ? 2: difference is 1 ?
  • 2 ? 3: difference is 1 ?
  • 3 ? 4: difference is 1 ?
  • 4 ? 6: difference is 2 ? (returns 6)

Additional Examples

// No gaps - returns null
const consecutiveArray = [1, 2, 3, 4, 5];
console.log("Consecutive array:", findFirstNonConsecutive(consecutiveArray));

// Multiple gaps - returns first one
const multipleGaps = [1, 2, 5, 6, 9, 10];
console.log("Multiple gaps:", findFirstNonConsecutive(multipleGaps));

// Empty array
const emptyArray = [];
console.log("Empty array:", findFirstNonConsecutive(emptyArray));

// Single element
const singleElement = [5];
console.log("Single element:", findFirstNonConsecutive(singleElement));
Consecutive array: null
Multiple gaps: 5
Empty array: null
Single element: null

Alternative Approach

Here's a more concise version using the find method:

const findFirstNonConsecutiveAlt = (arr = []) => {
    return arr.find((num, index) => {
        return index > 0 && num - arr[index - 1] !== 1;
    }) || null;
};

const testArray = [2, 3, 4, 7, 8, 9];
console.log("Alternative approach:", findFirstNonConsecutiveAlt(testArray));
Alternative approach: 7

Key Points

  • The function returns the first non-consecutive number, not its index
  • Returns null if all numbers are consecutive
  • Works with both positive and negative numbers
  • Handles edge cases like empty arrays and single elements

Conclusion

Finding the first non-consecutive number involves comparing adjacent elements and identifying where the sequence breaks. Both the loop-based and find-based approaches work effectively for detecting gaps in sequential data.

Updated on: 2026-03-15T23:19:00+05:30

458 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements