-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathequalize-the-array.go
72 lines (58 loc) · 1.13 KB
/
equalize-the-array.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
package main
import (
"fmt"
"sort"
)
type Pair struct {
Key int
Value int
}
type PairList []Pair
func (p PairList) Len() int {
return len(p)
}
func (p PairList) Less(i, j int) bool {
return p[i].Value < p[j].Value
}
func (p PairList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func main() {
var arrLen int
if m, err := fmt.Scan(&arrLen); m != 1 || (m < 1 || m > 100) {
panic(err)
}
uerSlice := make([] int, arrLen)
for i := 0; i < arrLen; i++ {
if m, err := fmt.Scan(&uerSlice[i]); m != 1 || (m < 1 || m > 100) {
panic(err)
}
}
fmt.Println(calcDeleteCount(findRepeat(uerSlice)))
}
func findRepeat(arr []int) map[int]int {
repeatMap := make(map[int]int)
for _, v := range arr {
repeatMap[v]++
}
return repeatMap
}
func rankByIntCount(intFrequencies map[int]int) PairList {
pl := make(PairList, len(intFrequencies))
i := 0
for k, v := range intFrequencies {
pl[i] = Pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
func calcDeleteCount(repeatMap map[int]int) int {
deleteCount := 0
for k, p := range rankByIntCount(repeatMap) {
if (k != 0) {
deleteCount += p.Value
}
}
return deleteCount
}