Skip to content

Instantly share code, notes, and snippets.

@letscodego
Last active August 13, 2022 07:04
Show Gist options
  • Save letscodego/245de117a56ed5e2ae036f918afd0d4a to your computer and use it in GitHub Desktop.
Save letscodego/245de117a56ed5e2ae036f918afd0d4a to your computer and use it in GitHub Desktop.
Longest Consecutive
public static int LongestConsecutive(int[] nums)
{
var hashset = new HashSet(nums);
var longest = 1;
foreach (var item in nums)
{
if (hashset.Contains(item - 1))
continue;
if (hashset.Contains(item + 1))
{
var length = 0;
while (hashset.Contains(item + length))
{
length++;
}
longest = Math.Max(longest, length);
}
}
return longest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment