<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p>

<p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [100,4,200,1,3,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest consecutive elements sequence is <code>[1, 2, 3, 4]</code>. Therefore its length is 4.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,3,7,2,5,8,4,6,0,1]
<strong>Output:</strong> 9
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:

        # This solution is O(n) despite how it initially appears
        # Linear because hash map does cancels iteration of redundant numbers

        #nums = [8, 3, 2, 5, 4, 6, 1, 1]
        visited = {num: False for num in nums}
        numsSet = set(nums)

        # Initialize max sequence length to 0
        maxCount = 0

        # Iterate through numbers in set
        for num in numsSet:

            # Don't iterate if already visited 
            # (already part of another, explored, sequence)
            if visited[num] == True:
                print(f"skip {num}")
                continue
            
            # initialize count of this sequence
            count = 1

            # check all numbers in sequence below num
            i = 1
            while num-i in numsSet:
                print(f"Found {num-i}")
                print(f"Making {num-i} true")
                visited[num-i] = True
                i += 1
                count += 1
                print(f"count: {count}")
                
            # check all numbers in sequence above num
            i = 1
            while num+i in numsSet:
                print(f"Found {num+i}")
                print(f"Making {num+i} true")
                visited[num+i] = True
                i += 1
                count += 1
                print(f"count: {count}")
            
            # update largest sequence length
            if count > maxCount:
                maxCount = count
                print(f"New max count: {maxCount}")
            

        return maxCount

        
39850