I need to store an array of Int array for ordered duplicates (which are in a Array).
Example:
mainArray = [7, 7, 3, 2, 2, 2, 1,
Another approach extending collections where elements are equatable
extension Collection where Element: Equatable {
var grouped: [[Element]] {
var result: [[Element]] = []
for element in self {
if element == result.last?.last {
result[result.index(before: result.endIndex)].append(element)
} else {
result.append([element])
}
}
return result
}
}
let array = [7, 7, 3, 2, 2, 2, 1, 7, 5, 5]
array.grouped // [[7, 7], [3], [2, 2, 2], [1], [7], [5, 5]]