4 ms·
This is the author's choice to make the problem hard -- Go has a built-in map type that works fine for this, and produces code as compact as the C++ version, an
by andrewla 8y ago
This is the author's choice to make the problem hard -- Go has a built-in map type that works fine for this, and produces code as compact as the C++ version, and is also O(n):
seen := make(map[string]bool)
for n, s := range array {
if seen[strings.ToLower(s)] {
array[n] = ""
} else {
seen[strings.ToLower(s)] = true
}
}
- earthboundkid 8y agoCode golf: https://play.golang.org/p/mZf5KoAN_87 https://play.golang.org/p/mZf5KoAN_87
- rndgermandude 8y agoDon't calculate strings.ToLower() twice and skip over s == "" to avoid the map lookups and some assignments when already tombstoned and you'll save a lot of time. seen := make(map[string]bool) for n, s := range array { if s == "" { continue } lower := strings.ToLower(s) if seen[lower] { array[n] = "" } else { seen[lower] = true } }
- minitech 8y agoHow can an element be already tombstoned? Isn’t that the first loop, making only one pass?