1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

refactor: change slice Contain logic, convert to map first, then check existence

This commit is contained in:
dudaodong
2022-07-22 10:22:34 +08:00
parent b5ba9ba573
commit 105ab49763

View File

@@ -14,13 +14,13 @@ import (
// Contain check if the value is in the slice or not // Contain check if the value is in the slice or not
func Contain[T comparable](slice []T, value T) bool { func Contain[T comparable](slice []T, value T) bool {
set := make(map[T]struct{}, len(slice))
for _, v := range slice { for _, v := range slice {
if v == value { set[v] = struct{}{}
return true
}
} }
return false _, ok := set[value]
return ok
} }
// ContainSubSlice check if the slice contain subslice or not // ContainSubSlice check if the slice contain subslice or not