# Check if items in an array are consecutive but WITHOUT SORTING in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument.

Our function should check if there is a sequence of n (taken as the second argument) or more consecutive numbers in the array but without sorting the array.

For example, if our input array is −

const arr = [0, 4, 6, 5, 9, 8, 9, 12];
const n = 3;

Then our function should return true because there exist three consecutive numbers 4, 5, and 6 in the array.

## Example

The code for this will be −

const arr = [0, 4, 6, 5, 9, 8, 9, 12];
const n = 3;
const findSequence = (arr, num) => {
if(num > arr.length){
return false;
};
let count = 1;
for(let i = 0; i < arr.length; i++){
let el = arr[i];
while(arr.includes(++el)){
count++;
if(count === num){
return true;
};
};
count = 1;
};
return false;
};
console.log(findSequence(arr, n));
console.log(findSequence(arr, 4));

## Output

And the output in the console will be −

true
false