4. Longest SubSequence

If asked in a technical interview, could you write a function in Swift to find the longest subsequence of array elements in consecutive order? The consecutive numbers can be in any order. For example:

//input
let sequence: Array<Int> = [9, 4, 10, 2, 7, 3, 11, 1]

//output = 4
the values 1, 2, 3, 4 are the longest sequence of consecutive elements


Hints

As shown, a consecutive sequence doesn’t necessarily require that our elements be in ascending or descending order. However, your solution will still need to be intuitive enough to recognize which values come in order. Could a brute force solution be designed to correctly sort through the array, or could the values be placed into a more efficient data structure?