I sort the array first, and then the solution is just a two pointers problem. This solution is much faster than the HashSet one. But I'm confused that sort array takes O(nlogn) time, while using HashSet takes O(n), why does this solution faster than HashSet? Thanks!

public int longestConsecutive(int[] nums) {

if(nums == null || nums.length == 0 || nums.length == 1) {

return nums.length;

}

Arrays.sort(nums);

```
int length = 0, start = 0, end = 1, n = 0;
while(end < nums.length) {
if(nums[end] == nums[end - 1]) {
while(end < nums.length - 1 && nums[end] == nums[end - 1]) {
end++;
}
}
if(nums[end] - nums[end - 1] != 1) {
start = end;
}
length = Math.max(nums[end] - nums[start] + 1, length);
end++;
}
return length;
}
```