 # Find consecutive number in array (JavaScript) // tell me it's right or wrong

consecutive number have may types

• Here is one of them +1 type
``````the first number is 1 and next number is 2 so it means its' 1++ -its' consecutive

if first number is 2 and next number is 5 so it means  it's not 2++ //(3) - it's not consecutive

if first is 1 and second is 2 and third id 3 and fourth is 4 -so it's all consecutive number
``````

basically it’s check last number++ and match with next

``````// consecutive number [ 1, 2 , 5 , 7, 9, 10, ..] here 1, 2 , 9, 10 are consecutive number

var conse_Num = [1, 2, 5, 6, 7, 9, 10, 13, 49, 50, 51, 52, 53   ,75, 78   ];
var filterConseNumber = [];

for (let i = 0; i <= conse_Num.length - 1; i++) {   //creating loop that's runing till array length value
let index1 = conse_Num[i];                      // geting all value using key
let nextIndex = conse_Num[i + 1];               // getting value as key if first key is 0 then here is 1 if firstis 1 here is 2

if (index1 + 1 == nextIndex) {   // checking if first key value is = 0 or next key value is = 1 or not //checking here consecutive number
filterConseNumber.push(index1, nextIndex);  // pushing matched value in array ;
}
}
console.log(filterConseNumber);

//removing duplicate number

for (let i = 0; i < filterConseNumber.length - 1; i++) { //creating loop that's runing till array length value
let index1 = filterConseNumber[i];                      // geting all value using key
let nextIndex = filterConseNumber[i + 1];               // getting value as key if first key is 0 then here is 1 if firstis 1 here is 2

if (index1 === nextIndex) {          // checking if frist key and seond key value are same or not
let findIndex = filterConseNumber.indexOf(nextIndex);   //finding index of this value we fliter using condition
console.log( " This Duplicate numer are removed ",nextIndex ); //finding index for duplicate value
filterConseNumber.splice(findIndex, 1);   //splice is predifine function that work on array, for add or delete value in specific key that you provide it
}

}
//removed

console.info("Here is a reult for consecutive number ", filterConseNumber);

``````

Another types

• +2 type // do as you want
``````//consecutive example [0, 2, 4, 6, 8, 10, 12, 40, 42 , 51, 53,55]

let consecutiveNumber = [0, 2, 10, 4, 6, 8, 10, 12, 40, 42 , 51, 53,55];
let result = [];

for (let i=0; i< consecutiveNumber.length-1; i++){
let firstKey = consecutiveNumber[i];
let nextKey = consecutiveNumber[i+1];

if( firstKey +2 === nextKey){
if( nextKey % 2 != 0){continue;} // if you want only number that's divided by 2 // beki sankhya
result.push(firstKey, nextKey);
}
}

console.log("Here is fillterd result for consecutive number " + result)

for (let i=0; i< result.length-1; i++){
let allKey = result[i];
let nextKey = result[i+1];
if (allKey == nextKey){
let deleteKey = result.indexOf(nextKey);
result.splice(deleteKey, 1);
}
}

console.log("Here is result for consecutive Number " + result);
``````

i will post some calculation base example for find value, that make more practice on it

both code is live on CodeSandox here is a link