Created
January 30, 2017 21:41
-
-
Save Zenithar/c5fe365c8a7c6c8a9b47c01e2ef2265b to your computer and use it in GitHub Desktop.
Three lists comparator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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