I often need to get rid of duplicates based on arbitrary equals function. I need implementation that:
- is fast and memory effective (does not create map)
- is reusable and easy to use, think of slice.Sort() (github.com/bradfitz/slice)
- it's not required to keep order of the original slice or preserve original slice
- would be nice to minimize copying
Can this be implemented in go? Why this function is not part of some library I am aware of? I was looking e.g. godash (github.com/zillow/godash) implementation uses map and does not allow arbitrary less and equal.
Here is how it should approximately look like. Test:
import (
"reflect"
"testing"
)
type bla struct {
ID string
}
type blas []bla
func (slice blas) Less(i, j int) bool {
return slice[i].ID < slice[j].ID
}
func (slice blas) EqualID(i, j int) bool {
return slice[i].ID == slice[j].ID
}
func Test_Unique(t *testing.T) {
input := []bla{bla{ID: "d"}, bla{ID: "a"}, bla{ID: "b"}, bla{ID: "a"}, bla{ID: "c"}, bla{ID: "c"}}
expected := []bla{bla{ID: "a"}, bla{ID: "b"}, bla{ID: "c"}, bla{ID: "d"}}
Unique(input, blas(input).Less, blas(input).EqualID)
if !reflect.DeepEqual(expected, input) {
t.Errorf("2: Expected: %v but was %v
", expected, input)
}
}
What I think will need to be used to implement this:
- Only slices as data structure to keep it simple and for easy sorting.
- Some reflection - the hard part for me! Since I am new to go.