Ex.
Input: var array: [Int] = [1, 2, 3, 1, 3, 4, 2, 1]
Output:
[[1, 1, 1], [2, 2], [3, 3], [4]]
A solution with Dictionary(grouping:by:)
let array = [1, 2, 3, 1, 3, 4, 2, 1]
let output = Dictionary(grouping: array, by: {$0})
.values
.sorted(by: { $0[0] < $1[0] })
Dictionary(grouping: array, by: { $0 }) .sorted(by: { $0.key < $1.key }) .map { $0.value }, performance-wise