This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/utils/list.go
2023-05-22 17:50:26 +02:00

28 lines
732 B
Go

package utils
// UniqueNonEmptyElementsOf returns a new slice containing unique non-empty elements from the input slice.
// It removes duplicate elements and empty strings while preserving the order of appearance.
func UniqueNonEmptyElementsOf(s []string) []string {
// Create a map to store unique elements
unique := make(map[string]bool)
// Create a new slice to store the unique non-empty elements
us := make([]string, 0, len(s))
for _, elem := range s {
// Skip empty strings
if len(elem) == 0 {
continue
}
// Check if the element is already in the unique map
if !unique[elem] {
// Add the element to the unique map and the new slice
unique[elem] = true
us = append(us, elem)
}
}
return us
}