2023-09-16 20:14:20 +08:00
|
|
|
package arrayx
|
|
|
|
|
|
|
|
|
|
// Author: yun
|
2023-11-10 00:05:27 +08:00
|
|
|
// Version: 2023年10月24日15:31:41
|
2023-09-16 20:14:20 +08:00
|
|
|
|
|
|
|
|
import (
|
2023-11-10 00:05:27 +08:00
|
|
|
"fmt"
|
2023-09-16 20:14:20 +08:00
|
|
|
"reflect"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否在一个数组里面
|
2023-11-10 00:05:27 +08:00
|
|
|
*
|
|
|
|
|
* 针对需要重复判断的情况,这个方法会比InArray()快一点
|
|
|
|
|
*
|
|
|
|
|
* @param target interface{} 目标值
|
|
|
|
|
* @param array interface{} 数组
|
|
|
|
|
* @return bool 是否存在
|
|
|
|
|
* @return int 索引
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
type inArray struct {
|
|
|
|
|
array interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewArray(array interface{}) *inArray {
|
|
|
|
|
return &inArray{array: array}
|
|
|
|
|
}
|
|
|
|
|
func (i *inArray) In(target interface{}) (exists bool, index int) {
|
|
|
|
|
exists = false
|
|
|
|
|
index = -1
|
|
|
|
|
switch reflect.TypeOf(i.array).Kind() {
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
s := reflect.ValueOf(i.array)
|
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
|
|
if reflect.DeepEqual(target, s.Index(i).Interface()) {
|
|
|
|
|
index = i
|
|
|
|
|
exists = true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否在一个数组里面
|
|
|
|
|
*
|
|
|
|
|
* @param target interface{} 目标值
|
|
|
|
|
* @param array interface{} 数组
|
|
|
|
|
* @return bool 是否存在
|
|
|
|
|
* @return int 索引
|
2023-09-16 20:14:20 +08:00
|
|
|
*/
|
|
|
|
|
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:待验证
|
2023-11-10 00:05:27 +08:00
|
|
|
// 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
|
|
|
|
|
// }
|
2023-09-16 20:14:20 +08:00
|
|
|
|
|
|
|
|
// 数组去重
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 00:05:27 +08:00
|
|
|
func ArrayUniqueInt64(target []int64) []int64 {
|
|
|
|
|
result := make([]int64, 0, len(target))
|
|
|
|
|
temp := map[int64]struct{}{}
|
|
|
|
|
for _, item := range target {
|
|
|
|
|
if _, ok := temp[item]; !ok {
|
|
|
|
|
temp[item] = struct{}{}
|
|
|
|
|
result = append(result, item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ArrayInt64ToString(target []int64) []string {
|
|
|
|
|
result := make([]string, 0)
|
|
|
|
|
for _, val := range target {
|
|
|
|
|
result = append(result, fmt.Sprintf("%d", val))
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ArrayIntToString(target []int) []string {
|
|
|
|
|
result := make([]string, 0)
|
|
|
|
|
for _, val := range target {
|
|
|
|
|
result = append(result, fmt.Sprintf("%d", val))
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-16 20:14:20 +08:00
|
|
|
// 交集
|
|
|
|
|
// 并集
|
|
|
|
|
// 差集
|
|
|
|
|
// 补集
|
|
|
|
|
// 去重
|
|
|
|
|
// 排序
|
|
|
|
|
// 反转
|
|
|
|
|
// 求和
|
|
|
|
|
// 求平均值
|
|
|
|
|
// 求最大值
|
|
|
|
|
// 求最小值
|
|
|
|
|
// 求最大值和最小值
|
|
|
|
|
// 求最大值和最小值的差值
|
|
|
|
|
// 求最大值和最小值的差值的绝对值
|
|
|
|
|
// 求最大值和最小值的差值的绝对值的平均值
|
|
|
|
|
// 求最大值和最小值的差值的绝对值的平均值的平方根
|
|
|
|
|
// 求最大值和最小值的差值的绝对值的平均值的平方根的平方
|
|
|
|
|
// 求最大值和最小值的差值的绝对值的平均值的平方根的平方的平均值
|