mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 14:12:28 +08:00
refactor: change slice Contain logic, convert to map first, then check existence
This commit is contained in:
@@ -14,13 +14,13 @@ import (
|
||||
|
||||
// Contain check if the value is in the slice or not
|
||||
func Contain[T comparable](slice []T, value T) bool {
|
||||
set := make(map[T]struct{}, len(slice))
|
||||
for _, v := range slice {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
set[v] = struct{}{}
|
||||
}
|
||||
|
||||
return false
|
||||
_, ok := set[value]
|
||||
return ok
|
||||
}
|
||||
|
||||
// ContainSubSlice check if the slice contain subslice or not
|
||||
|
||||
Reference in New Issue
Block a user