**22ms solution, beats 71.58%, O(n) run-time + O(n) extra space.**

Slam all those integers into a hash set.

Then use the hash set to check if sequences exist via. "while (mySet.find(nums[i]++) != mySet.end()) {length++;}" line.

The line "if (mySet.find(nums[i]-1) != mySet.end()) {continue;}" allows us to only look for numbers that begin a sequence.

Effectively eliminating the need to check a sequence unless its actually the beginning of one. This line alone is the reason why its O(n) run-time.

```
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> mySet(nums.begin(), nums.end());
int result = 0;
for (int i=0; i<nums.size(); i++)
{
if (mySet.find(nums[i]-1) != mySet.end()) //don't compare if the element is not the beginning of a sequence.
{
continue;
}
int length = 0;
while (mySet.find(nums[i]++) != mySet.end()) //we found an element that could be a beginning of a sequence, now loop thru the hash set for a sequence.
{
length++;
}
result = max (result, length);
}
return result;
}
};
```

**13 ms solution, beats 95.95%, O(nlogn) run-time and O(1) extra space.**

For some reason this one is faster, not sure what happened here. Also, probably one of the first solutions that should come to mind.

Basically sort the array, then go thru the array once counting for sequences. Very simple and straight forward.

```
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if (nums.size() == 0){
return 0;
}
sort(nums.begin(), nums.end());
int consecutiveCompare = nums[0]-1;
int counter = 0;
int longestCounter = 0;
for (int i=0; i<nums.size(); i++){
if (consecutiveCompare == nums[i]) { //handle duplicates
continue;
}
else if (consecutiveCompare+1 != nums[i]){ //found the end of a sequence, now reset the sequence
consecutiveCompare = nums[i];
longestCounter = max(longestCounter, counter);
counter = 1;
}
else { //found an element of a sequence, now continue to the next element.
consecutiveCompare = nums[i];
counter++;
}
}
longestCounter = max(longestCounter, counter);
return longestCounter;
}
};
```