import Foundation
func solution(_ k:Int, _ tangerine:[Int]) -> Int {
var tmp: [Int:Int] = [:]
var sum = 0
for tan in tangerine {
tmp[tan, default: 0] += 1
}
let sortedValue = tmp.sorted(by: { $0.value > $1.value })
var answer = 0
for (index, value) in sortedValue.enumerated() {
sum += value.value
if sum >= k {
answer = index + 1
break
}
}
return answer
}
func solution(_ k: Int, _ tangerine: [Int]) -> Int {
return Dictionary(grouping: tangerine) { $0 }.values
.sorted { $0.count > $1.count }
.reduce((0, 0)) { acc, array in acc.1 >= k ? acc : (acc.0 + 1, acc.1 + array.count) }
.0
}