Skip to content

Instantly share code, notes, and snippets.

@joeke80215
Created April 20, 2019 13:01
Show Gist options
  • Save joeke80215/129e0178c5409f1f02c0ab0dc9d0fac9 to your computer and use it in GitHub Desktop.
Save joeke80215/129e0178c5409f1f02c0ab0dc9d0fac9 to your computer and use it in GitHub Desktop.
golang max duplicate element
package main
import "fmt"
// MaxDup input a random slice and return max duplicate element and lenght
func MaxDup(a []int) (int, int) {
maxV := 0
max := 0
b := make(map[int]int)
for i := 0; i < len(a); i++ {
b[a[i]]++
if b[a[i]] > max {
max = b[a[i]]
maxV = a[i]
}
}
return maxV, max
}
func main() {
// output: 3 4
fmt.Println(MaxDup([]int{1, 3, 2, 4, 1, 4, 2, 32, 45, 3, 55, 3, 4, 5, 2, 3}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment