Algorithm/noj.am

[Swift]백준 1157 / 딕셔너리

SweetDev 2019. 11. 7. 20:22

import Foundation

let givenString = readLine()!.uppercased()

 

// 선언과 초기화

var dictionary: [Character: Int] = [:]

"ABCDEFGHIJKLMNOPQRSTUVWXYZ".forEach{ dictionary[$0] = 0 }

 

// 값 추가

for letter in givenString { dictionary[letter]! += 1 }

 

// 가장 큰 값 찾기

let biggest = dictionary.max{ a, b in a.value < b.value }

dictionary.removeValue(forKey: biggest!.key)

 

let secondBiggest = dictionary.max{ a, b in a.value < b.value }

 

if (biggest?.value == secondBiggest?.value) { print("?") }

else { print(biggest!.key) }

 

더 잘 짤 수 있을것같은데...🤔