Open In App

JavaScript Program to find the Longest Consecutive Sequence of Numbers in an Array

Last Updated : 15 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to find the longest consecutive sequence of numbers in an array using JavaScript language. We are given an array that consists of integers we need to find the longest subsequence such that elements in the subsequence are consecutive integers, these numbers are in any order.

Example:

Input: inputArray[] = [1, 9, 3, 10, 4, 20, 2]
Output: 4
Explanation: The subsequence 1, 3, 4, 2 is the longest subsequence of consecutive elements

Approaches to Find the Longest Consecutive Sequence of Numbers in an Array in JavaScript

  • Using JavaScript Set
  • Using Sorting in JavaScript
  • Using Brute Force

So, let’s see each of the approaches with its implementation.

Approach 1: Using JavaScript Set

  • In this approach, we use the JS Set that finds the longest consecutive subsequence. We take the array as the input and check if the array is empty, if it is not empty then we create the set of all elements in the input array.
  • Then we iterate through the input array. For each element, we are checking the predecessor is in the set, if it is not, then we are starting a new consecutive subsequence with that element.
  • We are keeping track of the length of the longest consecutive subsequence and printing it at the end.

Example: In this example, we will find the longest consecutive sequence of numbers in an array using Set in JavaScript.

Javascript




function longConsequtiveSeqUsingSet(inputarry) {
    if (inputarry == null || inputarry.length === 0) {
        return 0;
    }
    let set = new Set(inputarry);
    let outputLongValue = 0;
    for (let temp of inputarry) {
        if (!set.has(temp - 1)) {
            let count = 0;
            while (set.has(count + temp)) {
                count++;
            }
            outputLongValue = Math.max(
                outputLongValue,
                count
            );
        }
    }
    return outputLongValue;
}
let inputArray = [36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42];
console.log(longConsequtiveSeqUsingSet(inputArray));


Output

5

Time compexity: O(n)

Space compleity: O(n)

Approach 2: Using Sorting in JavaScript

  • In this apporach, we are using the Sorting apporach, were firstly we are checking whteger the array is empty, of not, then we are sorting the array in the asecending order.
  • After that, we are going thorugh all thw alements of the array by maintinag the count of the consequtib element.
  • The longest subsequence is the maxium count whilch will be printed at the end.

Example: In this example, we will find the longest consecutive sequence of numbers in an array using Sorting technique in JavaScript.

Javascript




function longConsequtiveSeqUsingSorting(inputarray) {
    if (inputarray.length === 0) {
        return 0;
    }
    inputarray.sort((a, b) => a - b);
    let countValue = 1;
    let maxcountValue = 1;
    for (let i = 1; i < inputarray.length; i++) {
        if (inputarray[i - 1] + 1 === inputarray[i]) {
            countValue++;
        } else if (inputarray[i - 1] !== inputarray[i]) {
            countValue = 1;
        }
        maxcountValue = Math.max(maxcountValue, countValue);
    }
    return maxcountValue;
}
const inputArray = [1, 9, 3, 10, 4, 20, 2];
const outputLongValue =
    longConsequtiveSeqUsingSorting(inputArray);
console.log(outputLongValue);


Output

4

Time Complexity: O(N * log(N))

Space Complexity: O(1)

Approach 3: Using Brute Force

  • In this approach, we are starightforwardly using the Brute Force in JavaScript, where we are starting from the each element of the input array and checking whrther it is the start of tje lomgest consecutive subsequence.
  • We are doing this by starting from the lement and adding all the consecutive elements to the subsequence till we are not reaching an element tht is not consecutive toots provious element.
  • We are tacking the longest consecutive subsequence and printing it.

Example: In this example, we will find the longest consecutive sequence of numbers in an array using Brute Force method in JavaScript.

Javascript




function longConsequtiveSeqUsingBrute(inputArray) {
    let longestLength = 0;
    for (let i = 0; i < inputArray.length; i++) {
        let currentNumValue = inputArray[i];
        let currentLengthValue = 1;
        while (inputArray.includes(currentNumValue + 1)) {
            currentNumValue += 1;
            currentLengthValue += 1;
        }
        if (currentLengthValue > longestLength) {
            longestLength = currentLengthValue;
        }
    }
    return longestLength;
}
const inputArray = [1, 9, 3, 10, 4, 20, 2];
const outputLongValue =
    longConsequtiveSeqUsingBrute(inputArray);
console.log(outputLongValue);


Output

4

Time Complexity: O(N^3)

Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads