Re:Removing Array Duplicates
by andrewla on Friday April 12, 2019 @09:35AM
(#19644629)
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):
[ Parent
| on HN ]
Re:Removing Array Duplicates
by rndgermandude on Friday April 12, 2019 @09:00PM
(#19650384)
Don'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
}
}How can an element be already tombstoned? Isn’t that the first loop, making only one pass?
Re:Removing Array Duplicates
by earthboundkid on Friday April 12, 2019 @09:59AM
(#19644825)
Code golf: https://play.golang.org/p/mZf5KoAN_87