-4

Ex.

Input: var array: [Int] = [1, 2, 3, 1, 3, 4, 2, 1]

Output:

[[1, 1, 1], [2, 2], [3, 3], [4]]

RajeshKumar R
  • 14,770
  • 2
  • 37
  • 67
  • Possible duplicate of [Removing duplicate elements from an array in Swift](https://stackoverflow.com/questions/25738817/removing-duplicate-elements-from-an-array-in-swift) – Maximelc Aug 12 '19 at 09:31

1 Answers1

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] })
Alexander
  • 54,014
  • 10
  • 87
  • 136
vadian
  • 253,546
  • 28
  • 306
  • 323