๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

๋”•์…”๋„ˆ๋ฆฌ์™€ 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: " "))
๋ฐ˜์‘ํ˜•