개발자 코드(Code)/Swift(문법)

Swift 06.컬렉션 set집합연산

Chain X 2020. 8. 20. 18:32
728x90
반응형
// Set : 집합연산
let setA: Set<Int> = [1,2,3,7,4]
let setB: Set<Int> = [5,3,2,8,9]

//print(setA.union(setB)) // 현재 랜덤 값

let unionSet = setA.union(setB)
print(unionSet.sorted()) //정렬해서 출력하기
print(setA.intersection(setB)) // 교집합
print(setA.subtracting(setB)) // 차집합


//Set의 멤버십과 동등 비교
let houseAnimals: Set = ["dog","cat"]
let farmAnimals: Set = ["cow","chicken","sheep","dog","cat"]
let cityAnimals: Set = ["duck","rabbit"]

print(houseAnimals.isSubset(of: farmAnimals)) // true

print(farmAnimals.isSuperset(of: houseAnimals)) // true
//아무 관계도 없음
print(farmAnimals.isDisjoint(with: cityAnimals)) // true
반응형

'개발자 코드(Code) > Swift(문법)' 카테고리의 다른 글

Swift) 08_옵셔널.Playground  (0) 2020.09.24
Swift) 07_반복문.Playground  (0) 2020.09.24
Swift 05. 컬렉션 Dictionaray  (0) 2020.08.20
Swift 04.컬렉션 Aarry  (0) 2020.08.20
Swift 03.기본 연산자  (0) 2020.08.20