Junior — Middle
Can you explain what a Set data structure is?
sobes.tech AI
Answer from AI
The data structure Set is a collection of unique elements, where each element can only appear once. Main characteristics:
- Uniqueness — duplicates are not allowed.
- Unordered — elements do not have a fixed order.
- Fast lookup — operations like adding, removing, and checking for an element are usually performed in constant time.
In iOS (Swift), Set is implemented as a generic type, for example:
var fruits: Set<String> = ["Apple", "Banana", "Orange"]
fruits.insert("Mango")
print(fruits.contains("Apple")) // true
Set is useful when you need to store a collection without repetitions and quickly check for the presence of elements.