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)