数组中重复的数字(Golang)
使用哈希表
package main
import "fmt"
func main() {
a := [...]int{2,3,1,0,2,5,3}
num := make(map[int]bool)
for _, v := range a {
if !num[v] {
num[v] = true
} else {
fmt.Println(v)
}
}
}
排序查找
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{2,3,1,0,2,5,3}
sort.Ints(a)
for i, v := range a {
if i != v {
if v == a[v] {
fmt.Println(v)
} else {
a[i], a[v] = a[v], a[i]
}
}
}
}