Sobes.tech
Junior — Middle

Mis on aja keerukus otsida elementi võtme järgi Map-andmestruktuuris?

sobes.tech AI

Vastus AI-lt

In Go, the data structure map is implemented based on a hash table. On average, searching for an element by key in a map has a time complexity of O(1) — constant time. However, in the worst case, for example, with many collisions, the complexity can degrade to O(n), where n is the number of elements in the map.

Example of using map in Go:

m := make(map[string]int)
m["apple"] = 5
value, ok := m["apple"]
if ok {
    fmt.Println("Value:", value)
}

Thus, for most practical tasks, searching in a map is very fast and efficient.