@@ -0,0 +1,50 @@
|
||||
package vidser
|
||||
|
||||
import (
|
||||
"91porn-server/models/v/vidmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// 元素去重
|
||||
func RemoveRep(slc []*vidmod.VideoModel) []*vidmod.VideoModel {
|
||||
if len(slc) < 1024 {
|
||||
// 切片长度小于1024的时候,循环来过滤
|
||||
return RemoveRepByLoop(slc)
|
||||
}
|
||||
// 大于的时候,通过map来过滤
|
||||
return RemoveRepByMap(slc)
|
||||
}
|
||||
|
||||
// 通过map主键唯一的特性过滤重复元素
|
||||
func RemoveRepByMap(slc []*vidmod.VideoModel) []*vidmod.VideoModel {
|
||||
result := make([]*vidmod.VideoModel, 0, len(slc))
|
||||
tempMap := map[primitive.ObjectID]struct{}{} // 存放已添加主键
|
||||
for _, e := range slc {
|
||||
if _, ok := tempMap[e.ID]; ok { // 主键已添加, 则不重复添加
|
||||
continue
|
||||
}
|
||||
tempMap[e.ID] = struct{}{}
|
||||
result = append(result, e)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 通过两重循环过滤重复元素
|
||||
func RemoveRepByLoop(slc []*vidmod.VideoModel) []*vidmod.VideoModel {
|
||||
result := make([]*vidmod.VideoModel, 0, len(slc)) // 存放结果
|
||||
for i := range slc {
|
||||
exists := false
|
||||
for j := range result {
|
||||
if slc[i].ID == result[j].ID {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if exists {
|
||||
continue
|
||||
}
|
||||
result = append(result, slc[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user