This is an exercise in a book which ask me to implement
Write an in-place function to eliminate adjacent duplicates in a []string slice.
I am relatively new to golang and I am not sure if my implementation is correct and effective or not.
func removeAdj(strings []string) []string {
for i := 0; i < len(strings); i++ {
dup := false
lastJ := i
for j := (i + 1); j < len(strings); j++ {
if strings[i] == strings[j] {
dup = true
lastJ = j
} else {
break
}
}
if dup {
strings[i] = ""
first := strings[:i]
second := strings[lastJ+1:]
strings = append(first, second...)
i = -1
}
}
return strings
}
Tests:
Input: [a z x x z y]
Output: [a y]
Input: [g e e k s f o r g e e g]
Output: [g k s f o r]
Input: [c a a a b b b a a c d d d d]
Output: []
Input: [a c a a a b b b a c d d d d]
Output: [a c a c]
[a]
since, after removing allb
s, you can removea
s and thenc
s. Can you clarify the problem statement and what hypothesis you took? \$\endgroup\$