面试题答案
一键面试func findHighestScoredNames(_ array: [[[String : Any]]]) -> [String] {
var result: [String] = []
for subArray in array {
var highestScore = Int.min
var highestScoredName = ""
for dict in subArray {
guard let score = dict["score"] as? Int else { continue }
if score > highestScore {
highestScore = score
if let name = dict["name"] as? String {
highestScoredName = name
}
}
}
result.append(highestScoredName)
}
return result
}
调用示例:
let twoDArray: [[[String : Any]]] = [
[["name": "Alice", "score": 85], ["name": "Bob", "score": 90]],
[["name": "Charlie", "score": 78], ["name": "David", "score": 88]]
]
let result = findHighestScoredNames(twoDArray)
print(result)