slice.go 368 B

1234567891011121314151617181920
  1. package slice
  2. func RemoveRepeatElement[T comparable](elements []T) []T {
  3. if elements == nil || len(elements) == 0 {
  4. return elements
  5. }
  6. result := make([]T, 0)
  7. tempMap := make(map[T]interface{})
  8. for _, element := range elements {
  9. l := len(tempMap)
  10. tempMap[element] = nil
  11. if len(tempMap) != l {
  12. result = append(result, element)
  13. }
  14. }
  15. return result
  16. }