Starting Point: We have an array [100, 4, 200, 1, 3, 2] and need to find the longest consecutive sequence.
Step 1/6: Initial Array
Step 1: Add all elements to a HashSet for O(1) lookups. This allows us to quickly check if a number exists without iterating through the array each time.
Step 2/6: Creating the HashSet
Step 2: For each number in the array, we check if it's a start of a sequence. A number is a start if (number-1) is not in the HashSet.
Step 3/6: Finding Sequence Start Points
Step 3: For the first starting point (1), we count consecutive elements by checking if (number+1) exists in the HashSet.
Step 4/6: Counting Sequence from 1
Step 4: Similarly, we check other starting points. For 100, we look for 101 (not found). For 200, we look for 201 (not found).
Step 5/6: Checking Other Starting Points
Step 5: The longest consecutive sequence is [1, 2, 3, 4] with a length of 4. This is our answer.
Step 6/6: Final Result