1、更新
This commit is contained in:
52
common/helper/extension_helper.go
Normal file
52
common/helper/extension_helper.go
Normal file
@ -0,0 +1,52 @@
|
||||
package helper
|
||||
|
||||
/*
|
||||
判断是否存在
|
||||
|
||||
- @arr 数组
|
||||
- @value 值
|
||||
*/
|
||||
func ArrayAny[T comparable](arr []T, value T) bool {
|
||||
for _, v := range arr {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 定义一个条件函数类型
|
||||
type ConditionFunc[T any] func(T) bool
|
||||
|
||||
/*
|
||||
判断是否存在
|
||||
|
||||
- @arr 数组
|
||||
|
||||
- @condition 判断函数
|
||||
|
||||
@return 对象指针
|
||||
*/
|
||||
func ArrayAnyExtension[T any](arr *[]T, condition ConditionFunc[T]) *T {
|
||||
for _, v := range *arr {
|
||||
if condition(v) {
|
||||
return &v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveDuplicates(nums []int64) []int64 {
|
||||
m := make(map[int64]bool)
|
||||
result := []int64{}
|
||||
|
||||
for _, num := range nums {
|
||||
if !m[num] {
|
||||
m[num] = true
|
||||
result = append(result, num)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user