Try to start from the lowest number of a streak, then test y = x + 1, x + 2, ..., until y is not in the set. Then the length would be simply y - x.
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int longest = 0;
for (int num : nums) {
if (!set.contains(num - 1)) {
int curLength = 1;
int curNum = num;
while (set.contains(num + 1)) {
curLength += 1;
num += 1;
}
longest = Math.max(longest, curLength);
}
}
return longest;
}