Best way to find the most common element in an array?

Hello,

We have an array of locations that’s passed in as data, would like to find the most common one in the array. Have ended up with this (where $locations is an array of possibly repeating location names)

{{- $uniq_locations := uniq $locations }}
{{- $locationMaxID := "" -}}
{{- $locationMaxCount := 0 -}}

{{- range $uniq_locations -}}
  {{- if gt (len (apply $locations "eq" . ".")) $locationMaxCount -}}
    {{- $locationMaxCount = len (apply $locations "eq" . ".") -}}
    {{- $locationMaxID = . -}}
  {{- end -}}
{{- end -}}

but would like to check am not missing something more obvious ?

thanks,

Richard