-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
51 lines (43 loc) · 896 Bytes
/
counter.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
package collections
import "sort"
type Counter struct {
Items map[float64]int
}
func NewCounter(sample []float64) *Counter {
if len(sample) == 0 {
panic("There is an empty sequence.")
}
counts := make(map[float64]int, len(sample))
for _, data := range sample {
if _, ok := counts[data]; !ok {
counts[data] = 1
} else {
counts[data]++
}
}
return &Counter{counts}
}
func (counter Counter) MaxValue() int {
maxValue := 0
for _, value := range counter.Items {
if value > maxValue {
maxValue = value
}
}
return maxValue
}
type set map[int]bool
func (counter Counter) Values() []int {
valuesSet := make(set)
for _, value := range counter.Items {
if _, ok := valuesSet[value]; !ok {
valuesSet[value] = true
}
}
values := make([]int, 0, len(valuesSet))
for key := range valuesSet {
values = append(values, key)
}
sort.Ints(values)
return values
}