mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-30 17:48:41 +01:00
20201203
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func goodPairsCount(src []int) int {
|
||||
count := 0
|
||||
l := len(src)
|
||||
for i := 0; i < l; i++ {
|
||||
for j := i + 1; j < l; j++ {
|
||||
if src[i] == src[j] {
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func goodPairsCount2(src []int) int {
|
||||
m := make(map[int]int)
|
||||
for _, n := range src {
|
||||
m[n] += 1
|
||||
}
|
||||
count := 0
|
||||
for _, v := range m {
|
||||
count += v * (v - 1) / 2
|
||||
}
|
||||
return count
|
||||
}
|
||||
Reference in New Issue
Block a user