๋์
๋๋ฆฌ์ sorted
๋ฅผ ์ด์ฉํด์ ํ์๋๋ฐ, ์ฝ๋๋ฅผ ๋ณด๋ฉด ์๊ฒ ์ง๋ง ๊ทธ๋ค์ง ๊นจ๋ํ์ง ์๋ค. ๊ทธ๋ฅ ์ด๋ฐ์์ผ๋ก ํ ์ ์๋ค~ ์ ๋๋ก๋ง ์ฐธ๊ณ ๋ฐ๋.
๋ฌธ์
https://www.acmicpc.net/problem/2910
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
let n = readLine()!.split(separator: " ").map { Int(String($0))! }
var message = readLine()!.split(separator: " ").map { Int(String($0))! }
var dict = [Int: Int]()
for m in message {
dict[m, default: 0] += 1
}
var answer = [Int]()
for (key, value) in dict.sorted(by: {
if $0.value == $1.value {
return message.firstIndex(of: $0.key)! < message.firstIndex(of: $1.key)!
}
return $0.value > $1.value
}) {
for _ in 0..<value {
answer.append(key)
}
}
print(answer.map().joined(separator: " "))