Skip to content

Instantly share code, notes, and snippets.

@Zenithar
Created January 30, 2017 21:41
Show Gist options
  • Save Zenithar/c5fe365c8a7c6c8a9b47c01e2ef2265b to your computer and use it in GitHub Desktop.
Save Zenithar/c5fe365c8a7c6c8a9b47c01e2ef2265b to your computer and use it in GitHub Desktop.
Three lists comparator
package threelist
// Compare two lists old vs new using fullset
func Compare(fullset, old, new []string) ([]string, []string, []string) {
// Initialize empty result lists
keep := []string{}
removed := []string{}
added := []string{}
// For each element of fullset
for _, el := range fullset {
// If known by old
if contains(old, el) {
if contains(new, el) {
keep = append(keep, el)
} else {
removed = append(removed, el)
}
} else {
// Not known by old
if contains(new, el) {
added = append(added, el)
}
}
}
return keep, removed, added
}
func contains(list []string, item string) bool {
set := make(map[string]struct{}, len(list))
for _, s := range list {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment