Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
293 lines
4.9 KiB
Go
293 lines
4.9 KiB
Go
package utils
|
|
|
|
import (
|
|
"math/rand"
|
|
"reflect"
|
|
)
|
|
|
|
//数组求交集,差集,并集
|
|
/*求并集**/
|
|
func Union(slice1, slice2 []string) []string {
|
|
m := make(map[string]int)
|
|
for _, v := range slice1 {
|
|
m[v]++
|
|
}
|
|
|
|
for _, v := range slice2 {
|
|
times, _ := m[v]
|
|
if times == 0 {
|
|
slice1 = append(slice1, v)
|
|
}
|
|
}
|
|
return slice1
|
|
}
|
|
|
|
// 求交集
|
|
func Intersect(slice1, slice2 []string) []string {
|
|
m := make(map[string]int)
|
|
nn := make([]string, 0)
|
|
for _, v := range slice1 {
|
|
m[v]++
|
|
}
|
|
|
|
for _, v := range slice2 {
|
|
times, _ := m[v]
|
|
if times == 1 {
|
|
nn = append(nn, v)
|
|
}
|
|
}
|
|
return nn
|
|
}
|
|
|
|
// 求交集
|
|
func IntersectInt64(slice1, slice2 []int64) []int64 {
|
|
m := make(map[int64]int)
|
|
nn := make([]int64, 0)
|
|
for _, v := range slice1 {
|
|
m[v]++
|
|
}
|
|
|
|
for _, v := range slice2 {
|
|
times, _ := m[v]
|
|
if times == 1 {
|
|
nn = append(nn, v)
|
|
}
|
|
}
|
|
return nn
|
|
}
|
|
|
|
// 求差集 slice1-并集
|
|
func Difference(slice1, slice2 []string) []string {
|
|
m := make(map[string]int)
|
|
nn := make([]string, 0)
|
|
inter := Intersect(slice1, slice2)
|
|
for _, v := range inter {
|
|
m[v]++
|
|
}
|
|
|
|
for _, value := range slice1 {
|
|
times, _ := m[value]
|
|
if times == 0 {
|
|
nn = append(nn, value)
|
|
}
|
|
}
|
|
return nn
|
|
}
|
|
|
|
// 求差集 slice1-并集
|
|
func DifferenceInt64(slice1, slice2 []int64) []int64 {
|
|
m := make(map[int64]int)
|
|
nn := make([]int64, 0)
|
|
inter := IntersectInt64(slice1, slice2)
|
|
for _, v := range inter {
|
|
m[v]++
|
|
}
|
|
|
|
for _, value := range slice1 {
|
|
times, _ := m[value]
|
|
if times == 0 {
|
|
nn = append(nn, value)
|
|
}
|
|
}
|
|
return nn
|
|
}
|
|
|
|
// 去重
|
|
func RemoveRepeat(slc []int64) []int64 {
|
|
result := []int64{} // 存放结果
|
|
for i := range slc {
|
|
flag := true
|
|
for j := range result {
|
|
if slc[i] == result[j] {
|
|
flag = false // 存在重复元素,标识为false
|
|
break
|
|
}
|
|
}
|
|
if flag { // 标识为false,不添加进结果
|
|
result = append(result, slc[i])
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func IncludeInt32(slc []int32, n int32) bool {
|
|
for _, v := range slc {
|
|
if v == n {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IncludeInt64(slc []int64, n int64) bool {
|
|
for _, v := range slc {
|
|
if v == n {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func RemoveInt64(slc []int64, n int64) []int64 {
|
|
for i := 0; i < len(slc); {
|
|
if slc[i] == n {
|
|
slc = append(slc[:i], slc[i+1:]...)
|
|
} else {
|
|
i++
|
|
}
|
|
}
|
|
return slc
|
|
}
|
|
|
|
func RemoveString(slc []string, n string) []string {
|
|
for i := 0; i < len(slc); {
|
|
if slc[i] == n {
|
|
slc = append(slc[:i], slc[i+1:]...)
|
|
} else {
|
|
i++
|
|
}
|
|
}
|
|
return slc
|
|
}
|
|
|
|
// RemoveRepeatString 去重string
|
|
func RemoveRepeatString(slc []string) []string {
|
|
result := []string{} // 存放结果
|
|
for i := range slc {
|
|
flag := true
|
|
for j := range result {
|
|
if slc[i] == result[j] {
|
|
flag = false // 存在重复元素,标识为false
|
|
break
|
|
}
|
|
}
|
|
if flag { // 标识为false,不添加进结果
|
|
result = append(result, slc[i])
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func IncludeString(slc []string, s string) bool {
|
|
for _, v := range slc {
|
|
if v == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func EqualInt64s(slice1, slice2 []int64) bool {
|
|
if len(slice1) != len(slice2) {
|
|
return false
|
|
}
|
|
for _, v1 := range slice1 {
|
|
find := false
|
|
for _, v2 := range slice2 {
|
|
if v1 == v2 {
|
|
find = true
|
|
break
|
|
}
|
|
}
|
|
if !find {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func EqualInt64WithOrder(slice1, slice2 []int64) bool {
|
|
if len(slice1) != len(slice2) {
|
|
return false
|
|
}
|
|
for k, v1 := range slice1 {
|
|
if slice2[k] != v1 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func EqualString(slice1, slice2 []string) bool {
|
|
if len(slice1) != len(slice2) {
|
|
return false
|
|
}
|
|
for _, v1 := range slice1 {
|
|
find := false
|
|
for _, v2 := range slice2 {
|
|
if v1 == v2 {
|
|
find = true
|
|
break
|
|
}
|
|
}
|
|
if find == false {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 切片乱序
|
|
func ShuffleSlice(slice interface{}) {
|
|
rv := reflect.ValueOf(slice)
|
|
if rv.Type().Kind() != reflect.Slice {
|
|
return
|
|
}
|
|
|
|
length := rv.Len()
|
|
if length < 2 {
|
|
return
|
|
}
|
|
swap := reflect.Swapper(slice)
|
|
//rand.Seed(time.Now().Unix())
|
|
for i := length - 1; i >= 0; i-- {
|
|
j := rand.Intn(length)
|
|
swap(i, j)
|
|
}
|
|
return
|
|
}
|
|
|
|
func RemoveOneInt64(slc []int64, n int64) []int64 {
|
|
for i := 0; i < len(slc); {
|
|
if slc[i] == n {
|
|
slc = append(slc[:i], slc[i+1:]...)
|
|
break
|
|
} else {
|
|
i++
|
|
}
|
|
}
|
|
return slc
|
|
}
|
|
|
|
// 求最大值
|
|
func SliceMaxInt64(slc []int64) int64 {
|
|
maxVal := slc[0]
|
|
for i := 1; i < len(slc); i++ {
|
|
//从第二个 元素开始循环比较,如果发现有更大的,则交换
|
|
if maxVal < slc[i] {
|
|
maxVal = slc[i]
|
|
}
|
|
}
|
|
return maxVal
|
|
}
|
|
|
|
// 求最大值
|
|
func SliceMinInt64(slc []int64) int64 {
|
|
minVal := slc[0]
|
|
for i := 1; i < len(slc); i++ {
|
|
//从第二个 元素开始循环比较,如果发现有更小的,则交换
|
|
if minVal > slc[i] {
|
|
minVal = slc[i]
|
|
}
|
|
}
|
|
return minVal
|
|
}
|
|
|
|
func ReverseAny(s interface{}) {
|
|
n := reflect.ValueOf(s).Len()
|
|
swap := reflect.Swapper(s)
|
|
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
|
|
swap(i, j)
|
|
}
|
|
}
|