Created
April 20, 2019 13:01
-
-
Save joeke80215/129e0178c5409f1f02c0ab0dc9d0fac9 to your computer and use it in GitHub Desktop.
golang max duplicate element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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