[Swift] Set (집합)

HeeelΒ·2022λ…„ 5μ›” 3일
0

Study-Swift 5.6

λͺ©λ‘ 보기
2/22

μ°Έκ³ μ‚¬μ΄νŠΈ:
The Swift Language Guide
Apple Developer


πŸ‘ŠπŸΎ Set

  • μˆœμ„œκ°€ μ—†μœΌλ©°, 각 μš”μ†Œ(elements) 듀이 독립적인 μ»¬λ ‰μ…˜μ΄λ‹€.

πŸ™ Declaration

@frozen struct Set<Element> where Element : Hashable
  • Set ν˜•νƒœλ‘œ μ €μž₯되기 μœ„ν•΄μ„œλŠ” νƒ€μž…μ΄ hashableμ΄μ–΄μ•Όλ§Œ ν•œλ‹€. hashable μ’…λ₯˜μ—λŠ” String, Int, Double, Bool λ“± κΈ°λ³Ένƒ€μž…μ΄ 해당됨.
    (hashable에 κ΄€ν•œ 글은 μΆ”ν›„ μž‘μ„± μ˜ˆμ •)

πŸ™ Set νŠΉμ§•

  1. μˆœμ„œκ°€ μ—†λŠ” μ»¬λ ‰μ…˜μ΄λ‹€. μ»¬λ ‰μ…˜μ„ μ‚¬μš©ν•  λ•Œ μˆœμ„œλ₯Ό κ³ λ €ν•˜μ§€ μ•Šμ„λ•Œ μ‚¬μš©ν•œλ‹€.
  2. μ»¬λ ‰μ…˜ λ‚΄μ—μ„œ 각 element μš”μ†Œλ“€μ΄ μ€‘λ³΅λœ κ°’ 없이 였직 ν•œκ°œλ§Œ μžˆλ‹€λŠ” 것을 보μž₯ν•  λ•Œ μ‚¬μš©ν•œλ‹€.

πŸ™ Set 생성

  • 빈 Set을 μƒμ„±ν•˜λŠ” 방법
// 동일 ν‘œν˜„
var ingredients: Set<String> = Set<String>()
var ingredients = Set<String>()
  • λ°°μ—΄ λ¦¬ν„°λŸ΄μ„ μ΄μš©ν•œ Set 생성
var ingredients: Set<String> = ["cocoa beans","sugar", "cocoa butter", "salt"]
  • λ‹€μŒκ³Ό 같이 νƒ€μž…μΆ”λ‘ μ„ μ΄μš©ν•˜μ—¬ μ„ μ–Έ κ°€λŠ₯
var ingredients: Set = ["cocoa beans","sugar", "cocoa butter", "salt"]

πŸ™ Set ν•¨μˆ˜

  • Set의 element개수 확인
print("I mixed \(ingredients.count) ingredients")
// I mixed 4 ingredients
  • Set λΉ„μ—ˆλŠ”μ§€ 확인
if ingredients.isEmpty{
    print("No ingredients")
} else{
    print("I have ingredients")
}
// "I have ingredients"
  • Set element 확인
if ingredients.contains("sugar"){
    print("No thanks, too sweet.")
} else {
    print("It's a delcious food")
}
// No thanks, too sweet.
  • Set μΆ”κ°€
ingredients.insert("water")
// ["water", "sugar", "cocoa beans", "cocoa butter", "salt"]
  • Set 제거
ingredients.remove("cocoa beans")
// ["cocoa butter", "sugar", "water", "salt"]

πŸ™ Set의 순회

  • for in loop을 μ΄μš©ν•΄ set을 μˆœνšŒν•  수 μžˆλ‹€.
for ingredient in ingredients{
    print(ingredient)
} 
//alt
//water
//sugar
//same

πŸ™ Set의 비ꡐ

  • 두 Set이 λ™μΌν•œ elements κ°€μ§€λŠ”μ§€ μ—°μ‚°μž("=")을 μ΄μš©ν•˜μ—¬ 비ꡐ κ°€λŠ₯
let someNumbersA: Set = [1,3,4,8,10]
let someNumbersB: Set = [1,3,4,8,10]

if someNumbersA==someNumbersB{
    print("same")
} else {
    print("different")
} 
// Prints "same"

πŸ™ Set의 ν™œμš©

var primes: Set = [2, 3, 5, 7]

// Tests whether primes is a subset of a Range<Int>
print(primes.isSubset(of: 0..<10))
// Prints "true"

// Performs an intersection with an Array<Int>
let favoriteNumbers = [5, 7, 15, 21]
print(primes.intersection(favoriteNumbers))
// Prints "[5, 7]"

πŸ™ Set의 μ •λ ¬

  • sorted λ©”μ†Œλ“œλ₯Ό μ‚¬μš© (μ˜€λ¦„μ°¨μˆœ)
let students: Set = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]

let sortedStudents = students.sorted()
print(sortedStudents)
// ["Abena", "Akosua", "Kofi", "Kweku", "Peter"]
  • λ‚΄λ¦Όμ°¨μˆœμ€ sorted λ©”μ†Œλ“œμ—μ„œ μ—°μ‚°μž(">")λ₯Ό μ΄μš©ν•œλ‹€.
let decendingStudents = students.sorted(by: >)
print(decendingStudents)
// ["Peter", "Kweku", "Kofi", "Akosua", "Abena"]

참고둜 이 Sorting Algorithm은 항상 Stableμƒνƒœλ₯Ό 보μž₯ν•˜μ§€ μ•ŠλŠ”λ‹€κ³  ν•œλ‹€.
(Swiftμ—μ„œ μ–΄λ–€ μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜λŠ”μ§€ 확인해야 ν• λ“―)
μ•„λ¬΄νŠΌ μ‹œκ°„ λ³΅μž‘λ„λŠ” (nlogn) n은 Set의 길이이닀.

0개의 λŒ“κΈ€