Created
August 7, 2019 15:12
-
-
Save devm33/6b686de217d8b937a507d03558ae3243 to your computer and use it in GitHub Desktop.
find items exclusive to two lists
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 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