Files
utils/arrayx/arrayx.go
T
2023-09-16 20:14:20 +08:00

78 lines
1.8 KiB
Go

package arrayx
// Author: yun
// Version: 2023年6月12日18:54:17
import (
"reflect"
"sort"
)
/**
* 判断是否在一个数组里面
*/
func InArray(target interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(target, s.Index(i).Interface()) {
index = i
exists = true
return
}
}
}
return
}
// 判断字符串是否在字符串数组中
// TODO:待验证
func InStringArray(target string, strArray []string) bool {
// 对字符串切片进行排序
sort.Strings(strArray)
index := sort.SearchStrings(strArray, target)
// 先判断 &&左侧的条件,如果不满足则结束此处判断,不会再进行右侧的判断
if index < len(strArray) && strArray[index] == target {
return true
}
return false
}
// 数组去重
func ArrayUniqueString(target []string) []string {
result := make([]string, 0, len(target))
temp := map[string]struct{}{}
for _, item := range target {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}
// 交集
// 并集
// 差集
// 补集
// 去重
// 排序
// 反转
// 求和
// 求平均值
// 求最大值
// 求最小值
// 求最大值和最小值
// 求最大值和最小值的差值
// 求最大值和最小值的差值的绝对值
// 求最大值和最小值的差值的绝对值的平均值
// 求最大值和最小值的差值的绝对值的平均值的平方根
// 求最大值和最小值的差值的绝对值的平均值的平方根的平方
// 求最大值和最小值的差值的绝对值的平均值的平方根的平方的平均值