[Swift] Collection Type - Set

Minjeong Park·2021년 10월 22일
0

Swift 문법

목록 보기
8/9

Set

Set은 순서가 존재하지 않고, 멤버가 유일한 것을 보장하는 데이터 컬렉션 타입이다.
원소의 삽입, 삭제, 갯수의 메소드는 Array와 동일하다.
하지만 Set에서는 순서 개념이 없기 때문에 index를 통해 접근하는 방식은 불가능하다.

var birds : Set<Character> = ["🦢","🐥","🕊"]
// method
birds.insert("🦜")
print(birds)
birds.remove("🐥")
print(birds)
birds.contains("🕊")

// property
birds.isEmpty
birds.count

var flyingBirds : Set<Character> = ["🕊","🦜","🦅"]

// 두 set을 합쳐(합집합) 새로운 Set을 만듦
birds.union(flyingBirds)

// 두 set을 빼서(차집합) 새로운 Set을 만듦
birds.subtract(flyingBirds)


var digitSet : Set<Int> = [1,2,3,4,5,5,5,5,6]
var integerSet : Set<Int> = [4,5,6,1,2,8,9,0]
// 두 set 중 공통되는 집합으로(교집합) 새로운 Set을 만듦
digitSet.intersection(integerSet)

공식 도큐먼트

시험기간이라 오늘은 여기까지 ,, ㅠㅠ

profile
아자아잣

0개의 댓글