Junior — Middle
Explain the concept of a dictionary in programming and its main functions.
sobes.tech AI
Answer from AI
A dictionary in programming is a data structure that stores pairs of 'key-value'. Each key is unique and associated with a specific value, allowing quick data retrieval by key.
Main functions of a dictionary:
- Fast search for a value by key.
- Adding new key-value pairs.
- Removing elements by key.
- Traversing all pairs for processing or output.
In Swift, a dictionary is represented by the type Dictionary<Key, Value>. Example:
var dict: [String: Int] = ["apple": 3, "banana": 5]
dict["orange"] = 7 // adding
let count = dict["apple"] // retrieving value by key