Given a mid-difficulty LeetCode-style problem, solve it in Python under interview constraints.
Implement longest_consecutive(nums), which returns the length of the longest sequence of consecutive integers in an unsorted array. Values may appear more than once, and the sequence does not need to preserve the array's order. Aim for O(n) average time.
Input is a list of integers. Output is an integer. Return 0 for an empty list.
def longest_consecutive(nums):