uniqueSlice.go 300 B

123456789101112131415
  1. package dto
  2. func UniqueSlice(slice []string) []string {
  3. seen := make(map[string]struct{}) // 使用空结构体节省内存
  4. result := []string{}
  5. for _, value := range slice {
  6. if _, ok := seen[value]; !ok {
  7. seen[value] = struct{}{}
  8. result = append(result, value)
  9. }
  10. }
  11. return result
  12. }