package main
import (
"fmt"
)
// DuplicateEle 重复元素
type DuplicateEle struct {
Value int
Index int
}
// FindDuplicate 查找数组中重复元素的方法
func FindDuplicate(arr []int) []*DuplicateEle {
indexMap := make([]int, 10)
dupRes := make([]*DuplicateEle, 0)
for index, ele := range arr {
indexMap[ele]++
if indexMap[ele] > 1 {
dEle := &DuplicateEle{
Index: index,
Value: ele,
}
dupRes = append(dupRes, dEle)
}
}
return dupRes
}
func main() {
arr := []int{1, 2, 3, 4, 5, 3, 4, 5, 2, 1, 7, 8}
dupRes := FindDuplicate(arr)
for _, dupEle := range dupRes {
fmt.Println(dupEle)
}
}
程序输出如下
图片.png
网友评论