Find the longest run of consecutive integers as [first, last]
A support desk hands out numbered tickets, but tickets get scanned in any order and some are scanned twice. The team lead wants to find the longest unbroken block of ticket numbers that were scanned, for example 1, 2, 3, 4, so they can archive it as one batch.
Implement longestConsecutiveRun(nums). It returns the longest run of consecutive integers that appear in nums, as a pair [first, last].
longestConsecutiveRun(nums) // => [first, last] | null
first, first + 1, ..., last that all appear somewhere in nums. Their positions in the array do not matter.first.[x, x].null for an empty array.nums.longestConsecutiveRun([102, 4, 100, 1, 101, 3, 2, 50]);
// => [1, 4] (1, 2, 3, 4 beats 100, 101, 102)
longestConsecutiveRun([3, 3, 2, 2, 1]);
// => [1, 3] (duplicates count once)
longestConsecutiveRun([20, 21, 1, 2, 10, 11]);
// => [1, 2] (three runs of length 2; 1 is the smallest first value)
longestConsecutiveRun([-2, 0, -1, 1, 5]);
// => [-2, 1]
longestConsecutiveRun([7]);
// => [7, 7]
longestConsecutiveRun([]);
// => null
nums contains safe integers, which can be negative. It can have up to 100,000 elements.