介绍
Golang 自身不带数组去重函数, 以下是自己实现的去重代码片段.
实现
去重分为有序和无序两种类型.
- 对数据顺序没有要求的情况下,先进行排序,再遍历去重.
- 要求数据顺序按原顺序排列的情况下,使用map留存已遍历的数据并用于校验即将访问到的数据.
基本类型
无序
func IntReduce(data []int) []int {
sort.Ints(data)
var lastData int
for key := 0; key < len(data); key++ {
val := data[key]
if key == 0 {
lastData = val
continue
}
if val == lastData {
data = append(data[:key], data[key+1:]...)
key--
}
lastData = val
}
return data
}
有序
func IntReduceOrder(data []int) []int {
hm := make(map[int]int, len(data))
for key := 0; key < len(data); key++ {
val := data[key]
if _, ok := hm[val]; ok {
data = append(data[:key], data[key+1:]...)
key--
continue
}
hm[val] = key
}
return data
}
运行结果
data = []int{1, 2, 3, 4, 5, 6, 8, 8, 9, 9, 7, 7, 1}
无序方案结果
[1 2 3 4 5 6 7 8 9]
有序方案结果
[1 2 3 4 5 6 8 9 7]
引用类型
样例数据结构
type UserBase struct {
Id int `json:"id"`
Username string `bson:"username"`
Age int `json:"age"`
City string `json:"city"`
Address string `json:"address"`
Country string `json:"country"`
}
type UserBases []UserBase
// 实现Golang的排序接口Sort
func (x UserBases) Len() int { return len(x) }
func (x UserBases) Less(i, j int) bool { return x[i].Id < x[j].Id }
func (x UserBases) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
无序
func UserBasesReduce(data UserBases) UserBases {
sort.Sort(data)
var lastData int
for key := 0; key < len(data); key++ {
userBase := data[key]
if key == 0 {
lastData = userBase.Id
continue
}
if userBase.Id == lastData {
data = append(data[:key], data[key+1:]...)
key--
}
lastData = userBase.Id
}
return data
}
有序
func UserBasesReduceOrder(data UserBases) UserBases {
hm := make(map[int]int, len(data))
for key := 0; key < len(data); key++ {
val := data[key]
if _, ok := hm[val.Id]; ok {
data = append(data[:key], data[key+1:]...)
key--
continue
}
hm[val.Id] = key
}
return data
}
运行结果
var data = []UserBase{
{Id: 1,Username: "First",Age: 10},
{Id: 5,Username: "Fifth",Age: 50},
{Id: 3,Username: "Third01",Age: 30},
{Id: 3,Username: "Third02",Age: 30},
{Id: 2,Username: "Second",Age: 20},
{Id: 4,Username: "Fourth",Age: 40},
{Id: 2,Username: "Second",Age: 20},
{Id: 4,Username: "Fourth",Age: 40},
}
无序方案结果
{Id:1 Username:First Age:10 City: Address: Country:}
{Id:2 Username:Second Age:20 City: Address: Country:}
{Id:3 Username:Third01 Age:30 City: Address: Country:}
{Id:4 Username:Fourth Age:40 City: Address: Country:}
{Id:5 Username:Fifth Age:50 City: Address: Country:}
有序方案结果
{Id:1 Username:First Age:10 City: Address: Country:}
{Id:5 Username:Fifth Age:50 City: Address: Country:}
{Id:3 Username:Third01 Age:30 City: Address: Country:}
{Id:2 Username:Second Age:20 City: Address: Country:}
{Id:4 Username:Fourth Age:40 City: Address: Country:}
原文地址:http://www.cnblogs.com/ryanrew/p/16858449.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性