package utility

// Unique - returns slice without duplicates
func Unique(slice []string) []string {
	keys := make(map[string]bool)
	var list []string
	for _, entry := range slice {
		if _, value := keys[entry]; !value {
			keys[entry] = true
			list = append(list, entry)
		}
	}
	return list
}

// Diff between first and second string slices. Reduces both slices to unique ones prior to making diff
func Diff(a, b []string) []string {
	a, b = Unique(a), Unique(b)
	mb := make(map[string]struct{}, len(b))
	for _, x := range b {
		mb[x] = struct{}{}
	}
	var diff []string
	for _, x := range a {
		if _, found := mb[x]; !found {
			diff = append(diff, x)
		}
	}
	return diff
}

// TwoSidedDiff difference between A set and B set plus difference between B set and A set.
func TwoSidedDiff(a, b []string) []string {
	var diff []string
	diff = append(diff, Diff(a, b)...)
	diff = append(diff, Diff(b, a)...)
	return diff
}

func Remove(s []string, i int) []string {
	s[i] = s[len(s)-1]
	return s[:len(s)-1]
}
