-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
  • 15,445
  • 2
  • 38
  • 70
  • 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
  • 59,041
  • 12
  • 98
  • 151
vadian
  • 274,689
  • 30
  • 353
  • 361