Longest Consecutive Sequence
Longest Consecutive Sequence
Let's play with numbers and discover sequences! In this challenge, you'll write a function to find the longest sequence of consecutive numbers in an array. It's like finding the longest chain in a necklace!
Imagine you have a list of numbers. They are all jumbled up, and your task is to find the longest series of numbers that appear consecutively. It’s almost like connecting the dots, but numerically!
For example, say you have the following list of numbers:
numbers = [100, 4, 200, 1, 3, 2]
The longest sequence of consecutive integers is [1, 2, 3, 4], so the function should return the length 4.
Here's another example:
numbers = [102, 101, 100, 5, 2, 3, 1, 4]
Although jumbled, the numbers from 1 to 5 make a consecutive sequence, giving us a length of 5.
Your mission, should you choose to accept it, is to implement the function longest_consecutive_sequence that returns the length of the longest consecutive elements sequence.
Examples
longest_consecutive_sequence([100, 4, 200, 1, 3, 2]) # returns 4 longest_consecutive_sequence([102, 101, 100, 5, 2, 3, 1, 4]) # returns 5 longest_consecutive_sequence([7, 6, 5, 4, 3, 2, 1]) # returns 7 longest_consecutive_sequence([10, 30, 20]) # returns 1
Have fun coding!
Inputs
Arg 1:
[100,4,200,1,3,2]Expected Output
4Actual Output