time complexity가 O(1)므로.
import Foundation
var arr = (readLine()!).split(separator: " ").map{Int(String($0))!}
let a = arr[0]
let b = arr[1]
var set = Dictionary<String,Bool>()
var set2 = Set()
var dividen:Double = 0
for indexn in 1...(a) {
set.updateValue(false, forKey: readLine()!)
}
for indexn in 1...(b) {
let given = readLine()!
if let _ = set[given] {
set2.insert(given)
}
}
var result = set2.sorted{$0<$1}
print(result.count)
for i in result{
print(i)
}
가 아래보다 좋다.
import Foundation
var arr = (readLine()!).split(separator: " ").map{Int(String($0))!}
let a = arr[0]
let b = arr[1]
var set = Set()
var set2 = Set()
var dividen:Double = 0
for indexn in 1...(a) {
set.insert(readLine()!)
}
for indexn in 1...(b) {
let given = readLine()!
if set.contains(given){
set2.insert(given)
}
}
var result = set2.sorted{$0<$1}
print(result.count)
for i in result{
print(i)
}