```
<pre>
public class Solution { <br/>
public int longestConsecutive(int[] nums) {<br/>
Set<Integer> set = new HashSet<>();
int max = 0;
for(int num : nums) set.add(num);
for(int num : nums) if (!set.contains(num-1)) {
int val = num;
while(set.remove(val++));
max = Math.max(max, val-num-1);
}
return max;
}
}
</pre>
```