leetcode-study

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        """
        Determine the length of the longest consecutive sequence in an unsorted array of integers.
        
        Parameters:
            nums (List[int]): A list of integers which can include positive, negative, and zero.
        
        Returns:
            int: The length of the longest sequence of consecutive numbers found in the list.
        """
        num_set = set(nums)  # Convert list to a set for O(1) lookups.
        longest = 0  # Initialize the variable to track the maximum consecutive sequence length.
        
        # Iterate over each unique number in the set.
        for num in num_set:
            # Check if the current number is the start of a sequence.
            # If num - 1 is not in the set, then num could be the beginning.
            if num - 1 not in num_set:
                current = num  # The starting number of a potential sequence.
                current_streak = 1  # The length of the current sequence, starting at 1.
                
                # While the next consecutive number exists in the set, extend the sequence.
                while current + 1 in num_set:
                    current += 1  # Move to the next consecutive number.
                    current_streak += 1  # Increment the current sequence length.
                
                # Update the longest consecutive sequence length if the current one is larger.
                longest = max(longest, current_streak)
        
        return longest

Summary of Techniques and Approaches: