Last active
August 13, 2022 07:04
-
-
Save CodeAboutNothing/245de117a56ed5e2ae036f918afd0d4a to your computer and use it in GitHub Desktop.
Longest Consecutive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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