Finding the Length of the Longest Consecutive Sequence in Javascript #leetcode 128
Introduction:
In this blog post, we will explore a JavaScript solution for finding the length of the longest consecutive sequence in an array of numbers. We will analyze the given code and explain its functionality step by step. Let's get started!
Problem:
Given an unsorted array of integers nums
, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n)
time.
/**
* @param {number[]} nums
* @return {number}
*/
const longestConsecutive = function(nums) {
const set = new Set(nums);
let longest = 0;
for (let num of nums) {
if (!set.has(num - 1)) {
let length = 0;
while (set.has(num + length)) {
length++;
longest = Math.max(longest, length);
}
}
}
return longest;
};
const nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1]
console.log(longestConsecutive(nums)); // output: 9
// Time O(n) & Space O(n)
Code Explanation:
The function longestConsecutive
, which takes an array of numbers (nums
) and returns the length of the longest consecutive sequence found in the array.
Creating a Set and Initializing Variables: The code begins by creating a new
Set
namedset
using the input arraynums
. This set will be used to efficiently check for the presence of numbers later on. It also initializes a variable namedlongest
to keep track of the length of the longest consecutive sequence found.Iterating through the Numbers: The code uses a
for...of
loop to iterate through each number in thenums
array. For each number (num
), it performs the following steps:Checking for the Start of a Consecutive Sequence: If the current number minus one (
num - 1
) is not present in the set, it indicates thatnum
is the start of a consecutive sequence. This check ensures that we only consider the starting numbers of the sequences and skip the numbers that might belong to an existing sequence.Calculating the Length of the Consecutive Sequence: Upon finding the start of a sequence, the code initializes a variable named
length
to 0. It then enters awhile
loop, which continues as long as the next number (num + length
) is present in the set. Within this loop, it incrementslength
by 1 for each consecutive number encountered. Additionally, it updates thelongest
variable to store the maximum length seen so far.Returning the Result: After the
for...of
loop finishes iterating through all the numbers, the function returns the value stored in thelongest
variable, representing the length of the longest consecutive sequence found in thenums
array.
Time and Space Complexity: The time complexity of the solution is O(n), where n is the number of elements in the nums
array, as we iterate through each element exactly once. The space complexity is also O(n) since we store the elements in a set.
Conclusion: In this blog post, we have examined a JavaScript solution for finding the length of the longest consecutive sequence in an array of numbers. By utilizing a set and efficient iteration, the code identifies the starting points of consecutive sequences and calculates their lengths. Understanding and implementing this solution can be valuable when dealing with similar sequence-related problems.
Thank you for reading, and happy coding!