Skip to content

Instantly share code, notes, and snippets.

@narusemotoki
Created May 2, 2014 04:51
Show Gist options
  • Save narusemotoki/6d2afb629ecc3f3aa184 to your computer and use it in GitHub Desktop.
Save narusemotoki/6d2afb629ecc3f3aa184 to your computer and use it in GitHub Desktop.
This is my first time go without hello world.
package main
import (
"fmt"
"sort"
)
func mode(arr []int) int {
sort.Ints(arr)
var mode_count, mode_value, current_count, current_value int
for _, a := range arr {
if a != current_value {
current_value, current_count = a, 0
}
current_count++
if mode_count < current_count {
mode_value, mode_count = current_value, current_count
}
}
return mode_value
}
func main() {
fmt.Println(mode([]int{1, 2, 2, 3, 3, 3, 2, 2}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment