Hashable
-
Swift Generic - 3Programming/Swift(iOS) 2021. 4. 6. 21:28
String 배열의 빈도수를 세는 프로그램을 짜 봅시다. hash로 빈도수를 세면 될것 같은데요..(python이면 collections의 defaultdict를 사용하겠지만..) let arr = [ "hello", "world", "hello", "world", "hello", "world", "show", "me", ] func printValues(_ values: [String]) { print(values.sorted()) var result = [String: Int]() for e in values { //result[e]가 존재하지 않으면 0, 존재하면 값 가져오는 부분 let v = result[e] ?? 0 result[e] = v + 1 } print(result) } 만약 Stri..