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:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user