Junior
Kuidas erineb massiiv kogumist (Set)?
sobes.tech AI
Vastus AI-lt
Array (Array) represents an ordered collection of values of the same type, allowing duplicates. Elements are accessible by index.
Set (Set) is an unordered collection of unique hashable values. Duplicates are not allowed.
Key differences:
| Feature | Array | Set |
|---|---|---|
| Order | Yes | No |
| Duplicates | Allowed | Not allowed |
| Access | By index | Element existence check |
| Performance | Fast access by index, slow add/remove in the middle | Fast add/remove and existence check operations |
Creation example:
// Array
var numbersArray: [Int] = [1, 2, 2, 3, 4]
// Set
var numbersSet: Set<Int> = [1, 2, 3, 4]