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/pkg/util/list.go

28 lines
731 B
Go
Raw Normal View History

package util
// 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
}