김경록의 앱 개발 여정

[Swift 백준] 1269 대칭 차집합 본문

Algorithm

[Swift 백준] 1269 대칭 차집합

Kim Roks 2025. 4. 10. 11:00

 

 

풀이

두 집합 a,b 에서

(a - b).count + (b-a).count 를 구하는 문제입니다.

Swift에서 여집합을 구하는 함수는 'subtract()''subtracting()' 입니다.

둘의 차이점은 

subtract subtracting
원본 변형 새로운 집합 return

이니 편의를 위해 'subtracting'을 사용해줍니다

 

let nm = readLine()!.split(separator: " ").map{Int($0)!}

let a = Set(readLine()!.split(separator: " ").map{Int($0)!})

let b = Set(readLine()!.split(separator: " ").map{Int($0)!})

let aCount = a.subtracting(b).count
let bCount = b.subtracting(a).count

print(aCount + bCount)

 

'Algorithm' 카테고리의 다른 글

[Swift 백준] 2212 센서  (0) 2025.04.15
[Swift 백준] 10610 30  (0) 2025.04.14
[Swift 백준] 11052 카드 구매하기  (0) 2025.04.03
[Swift 백준] 1475 방 번호  (0) 2025.03.31
[Swift 백준] 9465 스티커  (0) 2025.03.28