12345678910111213141516171819202122232425262728293031323334 |
- package slice
- func RemoveRepeatElement[T comparable](elements []T) []T {
- if elements == nil || len(elements) == 0 {
- return elements
- }
- result := make([]T, 0)
- tempMap := make(map[T]interface{})
- for _, element := range elements {
- l := len(tempMap)
- tempMap[element] = nil
- if len(tempMap) != l {
- result = append(result, element)
- }
- }
- return result
- }
- func ToMap[S any, K comparable, V any](slice []S, fn func(sliceValue S) (K, V, error)) (map[K]V, error) {
- m := make(map[K]V, len(slice))
- for _, v := range slice {
- key, value, err := fn(v)
- if err != nil {
- return nil, err
- }
- m[key] = value
- }
- return m, nil
- }
|