JavaScript Program to find the Longest Consecutive Sequence of Numbers in an Array
Last Updated :
15 Sep, 2023
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.
- 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));
Time compexity: O(n)
Space compleity: O(n)
- 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);
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);
Time Complexity: O(N^3)
Space Complexity: O(1)
Similar Reads
JavaScript Program to Find the length of the Longest Continuous Decreasing Subarray Given an array of numbers, our task is to find the length of the longest continuous subarray where the numbers are strictly decreasing. Example: Input:const arr = [7, 4, 7, 6, 5, 4, 6, 8, 9]Output: 4Explanation: The longest continuous decreasing subarray in this array is [7, 6, 5, 4], and its length
3 min read
Javascript Program to Find the Longest Bitonic Subsequence | DP-15 Given an array arr[0 ... n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as argument and returns the length of the longest bitonic subsequence. A sequence, sorted in increasing order is con
3 min read
Longest Increasing Subarray of Composite Numbers in Array Given an array of integers arr[], the task is to find the longest increasing subarray of composite numbers in an array. Examples: Input: arr[] = [1, 4, 7, 6, 8, 10, 12]Output: [6, 8, 10, 12]Explanation: The longest increasing subarray of composite numbers in the given array is [6, 8, 10, 12], which
12 min read
Longest Increasing Subsequence(LIS) using two arrays Given two arrays A[] and B[] of size N. Your Task is to find Longest Increasing Subsequence(LIS) in another array C[] such that array C[] is formed by following rules: C[i] = A[i] or C[i] = B[i] for all i from 1 to N.Examples: Input: A[] = {2, 3, 1}, B[] = {1, 2, 1}Output: 2Explanation: Form C[] as
9 min read
Javascript Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Longest Subsequence with absolute difference of pairs as at least Subsequence's maximum Given an array arr[] of length N. The task is to find the length of the longest subsequence of the array such that the absolute difference between any pair of elements is greater than or equal to the maximum element in that subsequence. Examples: Input: N = 6, arr[] = {1, 1, 0, 0, 0, 0}Output: 4Expl
7 min read
How to Find the Maximum Element in an Array? In Java, the array is a data structure that allows the users to store data of the same type in contiguous memory locations. To find the maximum element in an Array in Java, we can sort the array in ascending order using the Arrays.sort() method and then we can access the last element of the array wh
1 min read
Longest Increasing Subsequence in C In this article, we will learn how to find the Longest Increasing Subsequence (LIS) of a given sequence using C programming language. LIS is the longest subsequence of a sequence such that all elements of the subsequence are sorted in increasing order.Example:Input:Sequence: [10, 22, 9, 33, 21, 50,
9 min read
Longest Increasing Subsequence in C++ In this article, we will learn the concept of the Longest Increasing Subsequence (LIS) in a given sequence using C++ language. The LIS problem is about finding the longest subsequence of a sequence in which the elements are in sorted order, from lowest to highest, and not necessarily contiguous.Exam
9 min read
Moonfrog Labs Interview Experience | Set 3 Q1. Given a sequence of integers, find the longest increasing subsequence. Example: arr = [1, 2, 5, 3, 7] ans : [1, 2, 5, 7] or [1, 2, 3, 7] arr = [4, 3, 1, 2] ans: [1, 2]. Solution: Java import java.util.Arrays; /** @author hiccup */ class LIS { static int[] maxLIS; static int[] result; public stat
3 min read