for
HashSet

而Golang并没有这种高级的容器。只是找了一个大神实现的,稍微改了一下,能够支持字符串检测。

http://play.golang.org/p/_FvECoFvhq

type HashSet struct {
	set map[string]bool
}

func NewHashSet() *HashSet {
	return &HashSet{make(map[string]bool)}
}

func (set *HashSet) Add(i string) bool {
	_, found := set.set[i]
	set.set[i] = true
	return !found //False if it existed already
}

func (set *HashSet) Get(i string) bool {
	_, found := set.set[i]
	return found //true if it existed already
}

func (set *HashSet) Remove(i string) {
	delete(set.set, i)
}

内部使用map来保存哈希结果。而哈希函数直接就是使用字符串作为哈希结果。

######参考文献 + 【1】判断int数组中的元素是否重复 - 百度知道 + 【2】Sets Data Structure in Golang - StackExchange + 【3】java里有没有专门判断List里有重复的数据?最好能知道是第几行重复. - CSDN论坛

有疑问加站长微信联系(非本文作者)