Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AhmedMenaim/365fafc56bbd63dd93cb751619166c6b to your computer and use it in GitHub Desktop.
Save AhmedMenaim/365fafc56bbd63dd93cb751619166c6b to your computer and use it in GitHub Desktop.
Menaim Academy - Swift Course - Collection Types - Sets
import Foundation
// MARK: - What is the Set !?
// Unarranged list , perform operations,
// MARK: - Create Set
var letters = Set<Character>()
var names: Set<String> = ["ahmed", "mohamed", "Fawzy", "Menaim"]
print(names)
var animals: Set = ["Dog", "Cat"]
// MARK: - Accessing and Modifying a Set
animals.insert("Horse")
print(animals)
print(animals.count)
if animals.isEmpty { // if animals.count == 0
print("isEmpty")
}
else {
print("Full")
}
names.remove("Fawzy")
print(names)
// MARK: - Set Operations
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let allNumbers = evenDigits.union(oddDigits)
allNumbers.sorted()
evenDigits.intersection(oddDigits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment