-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (48 loc) · 1.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"log"
"os"
"regexp"
"time"
)
func main() {
cfg, err := LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
emails, err := generateEmails(cfg.GenerateEmailCount, cfg.DuplicatePercentage)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
start := time.Now()
dedupedEmails := dedupe(emails)
elapsed := time.Since(start)
log.Printf("Took %s to remove %v duplicate emails from %v emails", elapsed, len(emails)-len(dedupedEmails), len(emails))
}
func dedupe(emails []string) []string {
keys := make(map[string]bool)
deDupedEmails := []string{}
for i, email := range emails {
if !isValidEmail(email) {
remove(emails, i)
continue
} else {
if _, value := keys[email]; !value {
keys[email] = true
deDupedEmails = append(deDupedEmails, email)
}
}
}
return deDupedEmails
}
func isValidEmail(email string) bool {
isValid, _ := regexp.MatchString("[^@]+@[^@]+\\.[^@]+", email)
return isValid
}
// This is slow, but we want to preserve the order of the original list
func remove(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}