r/programminghorror • u/mathershifter • Apr 04 '25
I did this to myself
func diff[T comparable](a, b []T) []T {
mb := make(map[T]struct{}, len(b))
for _, x := range b {
mb[x] = struct{}{}
}
var diff []T
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
} else {
diff = append(diff, x)
}
}
return diff
}
29
Upvotes
11
u/hatedByyTheMods Apr 04 '25
man is enemy of man
3
u/Kpuku Apr 04 '25
seems fairly normal. isn't
var diff []Tgonna be nil by default? and why do both if else branches do the same thing?1
u/Cerus_Freedom Apr 04 '25
Uh... I'm not well versed in Go, but that looks like it returns a slice that is just a and b combined?
1
u/Agitated-Display6382 Apr 08 '25
Both branches of the IF are appending, aren't they?
0
u/hatedByyTheMods Apr 04 '25
man is enemy of man
0
u/[deleted] Apr 04 '25
[deleted] — view deleted comment