Longest Consecutive Sequence
Given an unsorted array of integers nums
, find the length of the longest sequence of consecutive elements.
Constraints
n ≤ 100,000
wheren
is the length ofnums
https://binarysearch.com/problems/Longest-Consecutive-Sequence
Examples
Example 1
Input
- nums =
[100, 4, 200, 1, 3, 2, 101, 105, 103, 102, 104]
Output
- answer =
6
Explanation
The longest sequence of consecutive elements is [100, 101, 102, 103, 104, 105]
. so we return its length: 6
.
Example 2
Input
- nums =
[100, 4, 200, 1, 3, 2]
Output
- answer =
4
Explanation
The longest sequence of consecutive elements is [1, 2, 3, 4]
. so we return its length: 4
.
Leave a comment