코딩테스트
신고 결과 받기
jgkim1008
2022. 3. 18. 08:54
코드
import Foundation
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
var dict: [String: [String]] = [:]
var result = Array(repeating: 0, count: id_list.count)
var countDict: [String:Int] = [:]
for (index, id) in id_list.enumerated() {
countDict[id] = index
}
for repo in report {
let arr = repo.split(separator: " ").compactMap {String($0)}
let beAccused = arr[1]
let accusation = arr[0]
if dict[beAccused] == nil {
dict.updateValue([accusation], forKey: beAccused)
} else {
if !dict[beAccused]!.contains(accusation) {
dict[beAccused]!.append(accusation)
}
}
}
for i in dict.keys {
if dict[i]!.count >= k {
for n in dict[i]! {
result[countDict[n]!] += 1
}
}
}
return result
}
배운점
- Array(repeating:,count:) 를 통해 배열안에 있는 요소의 갯수를 지정 가능
- 딕셔너리의 인덱스를 지정할수 있다.