To find the mode of all keys in a map in Go, we need to first determine the frequency of occurrence of each key in the map. We can do this by creating a new map where the keys are the values of the original map, and the values are the frequency of each key in the original map:
main.go517 chars24 lines
This function takes in a map with keys of type interface{}
and values of type interface{}
, and returns a slice of all keys with the highest frequency of occurrence.
To use this function:
main.go142 chars10 lines
In this example, the keys "b" and "e" both have a frequency of 2, which is the highest frequency in the map, so they are returned as the mode keys.
gistlibby LogSnag