I would change this to max($keyValuesByWeight). Although this is faster, it implies that one has the knowledge that frequency distribution returns the key-values ordered by value in descending order.