golang取两数组交集

# golang / 134访问 / 0评论 / 2021-09-15
//取数组交集
func ArrayIntersect(array1 interface{}, othersParams ...interface{}) ([]interface{}, error) {
	_array1 := gconv.SliceAny(array1)
	_othersParams := gconv.SliceAny(othersParams)
	if len(_array1) == 0 {
		return []interface{}{}, nil
	}
	if len(_array1) > 0 && len(othersParams) == 0 {
		return _array1, nil
	}
	var tmp = make(map[interface{}]int, len(_array1))
	for _, v := range _array1 {
		tmp[v] = 1
	}
	for _, param := range _othersParams {
		for _, arg := range gconv.SliceAny(param) {
			if tmp[arg] != 0 {
				tmp[arg]++
			}
		}
	}
	var res = make([]interface{}, 0, len(tmp))
	for k, v := range tmp {
		if v > 1 {
			res = append(res, k)
		}
	}
	return res, nil
}

本文地址: https://www.kumaomao.cn/article/LEIAWXSCLW.html