-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdedup.go
104 lines (98 loc) · 1.57 KB
/
dedup.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package venn
import (
"sync"
)
func deDuplicateString(a1 []string) []string {
r := make([]string, 0, len(a1))
wg := new(sync.WaitGroup)
inpOutCh := make(chan string)
lookup := make(stringLookUp)
// send data
wg.Add(1)
go func() {
defer func() {
close(inpOutCh)
wg.Done()
}()
for _, data := range a1 {
inpOutCh <- data
}
}()
// remove duplicates
wg.Add(1)
go func() {
defer func() {
wg.Done()
}()
// TODO: most expensive operation find alternative
for v := range inpOutCh {
if _, ok := lookup[v]; !ok {
lookup[v] = struct{}{}
r = append(r, v)
}
}
}()
wg.Wait()
// read inp
return r
}
func deDuplicateint(a1 []int) []int {
r := make([]int, 0, len(a1))
lookUp := map[int]bool{}
wg := new(sync.WaitGroup)
inCh := make(chan int)
wg.Add(1)
go func() {
defer func() {
close(inCh)
wg.Done()
}()
for _, data := range a1 {
inCh <- data
}
}()
wg.Add(1)
go func() {
defer func() {
wg.Done()
}()
for v := range inCh {
if ok := lookUp[v]; !ok {
lookUp[v] = true
r = append(r, v)
}
}
}()
wg.Wait()
return r
}
func deDuplicatefloat64(a1 []float64) []float64 {
r := make([]float64, 0, len(a1))
lookUp := map[float64]bool{}
wg := new(sync.WaitGroup)
inCh := make(chan float64)
wg.Add(1)
go func() {
defer func() {
close(inCh)
wg.Done()
}()
for _, data := range a1 {
inCh <- data
}
}()
wg.Add(1)
go func() {
defer func() {
wg.Done()
}()
for v := range inCh {
if ok := lookUp[v]; !ok {
lookUp[v] = true
r = append(r, v)
}
}
}()
wg.Wait()
return r
}