Last active
August 15, 2018 00:55
-
-
Save grahamannett/da439bfce398e5819154fc8dcb28c1e0 to your computer and use it in GitHub Desktop.
find most repeating int
This file contains 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" | |
"os" | |
"sort" | |
) | |
type kv struct { | |
Key string | |
Value int | |
} | |
func main() { | |
b := make(map[string]int) | |
for _, num := range os.Args[1:] { | |
if _, ok := b[num]; ok { | |
b[num]++ | |
} else { | |
b[num] = 1 | |
} | |
} | |
var ss []kv | |
for k, v := range b { | |
ss = append(ss, kv{k, v}) | |
} | |
sort.Slice(ss, func(i, j int) bool { | |
return ss[i].Value > ss[j].Value | |
}) | |
fmt.Println(ss[0]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment