Skip to content

Commit 8082c39

Browse files
committed
Create 128-Longest-Consecutive-Sequence.cs
1 parent 6dae314 commit 8082c39

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public int LongestConsecutive(int[] nums) {
3+
if (nums.Length < 2) return nums.Length;
4+
5+
var set = new HashSet<int>(nums);
6+
var longest = 0;
7+
foreach (var n in set)
8+
{
9+
if (!set.Contains(n-1))
10+
{
11+
var length = 0;
12+
while (set.Contains(n+length))
13+
{
14+
length++;
15+
longest = Math.Max(longest, length);
16+
}
17+
}
18+
}
19+
20+
return longest;
21+
}
22+
}

0 commit comments

Comments
 (0)