数组中重复的数字(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]
			}
		}
	}
}

posted @ 2019-08-17 17:50  darylc  阅读(874)  评论(0编辑  收藏  举报