From 105ab497632037e5734fe2814c4dd21a881684c2 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 22 Jul 2022 10:22:34 +0800 Subject: [PATCH] refactor: change slice Contain logic, convert to map first, then check existence --- slice/slice.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/slice/slice.go b/slice/slice.go index 543a334..f140e99 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -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