Algorithm/Baekjoon
[Swift] 백준 1764: 듣보잡
내일은개발천재🎵
2022. 4. 15. 22:09
문제 사이트
생각 과정
- 집합연산을 사용하자!
알고리즘
- 듣도 못한 사람의 이름을 Set에 저장한다.
- 보도 못한 사람의 이름을 Set에 저장한다.
- 두 Set의 합집합 = 듣도 보도 못한 사람
- 합집합 결과를 Array로 저장한 후 sort하여 출력한다.
나의 코드
let inputData: [Int] = readLine()!.split(separator: " ").map{ Int($0)! }
var setA = Set<String>()
var setB = Set<String>()
for _ in 0..<inputData[0]{
setA.insert(readLine()!)
}
for _ in 0..<inputData[1]{
setB.insert(readLine()!)
}
let result = Array(setA.intersection(setB)).sorted()
print(result.count)
for i in result{
print(i)
}