Skip to content

Instantly share code, notes, and snippets.

@devm33
Created August 7, 2019 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devm33/6b686de217d8b937a507d03558ae3243 to your computer and use it in GitHub Desktop.
Save devm33/6b686de217d8b937a507d03558ae3243 to your computer and use it in GitHub Desktop.
find items exclusive to two lists
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
a := getList("a.txt")
b := getList("b.txt")
fmt.Println(exclusive(a, b))
}
func getList(filename string) []string {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
r := []string{}
s := bufio.NewScanner(f)
for s.Scan() {
l := s.Text()
r = append(r, strings.TrimSpace(l))
}
return r
}
func exclusive(a, b []string) ([]string, []string) {
ma := make(map[string]bool, len(a))
mb := make(map[string]bool, len(b))
mm := make(map[string]bool)
for _, v := range a {
ma[v] = true
mm[v] = true
}
for _, v := range b {
mb[v] = true
mm[v] = true
}
nota := []string{}
notb := []string{}
for v, _ := range mm {
if _, ok := mb[v]; !ok {
notb = append(notb, v)
}
if _, ok := ma[v]; !ok {
nota = append(nota, v)
}
}
return nota, notb
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment