Files
sablier/pkg/array/remove_elements.go
Alexis Couvreur 4122109349 wip lol
2024-11-16 22:49:35 -05:00

22 lines
731 B
Go

package array
// RemoveElements returns a new slice containing all elements from `allElements` that are not in `elementsToRemove`
func RemoveElements(allElements, elementsToRemove []string) []string {
// Create a map to store elements to remove for quick lookup
removeMap := make(map[string]struct{}, len(elementsToRemove))
for _, elem := range elementsToRemove {
removeMap[elem] = struct{}{}
}
// Create a slice to store the result
result := make([]string, 0, len(allElements)) // Preallocate memory based on the size of allElements
for _, elem := range allElements {
// Check if the element is not in the removeMap
if _, found := removeMap[elem]; !found {
result = append(result, elem)
}
}
return result
}