Most frequent element
Write a function that takes a collection and returns the most frequent element. But here's the thing: you can't use the built-in function clojure.core/frequencies
. And if there are ties, just pick one.
Examples
(most-frequent [2 2 3 4 4 2 1 1 3 2]) ;=> 2
(most-frequent []) ;=> nil
(most-frequent [1 1 4 4 5]) ;=> 4
Notes
- return
nil
for an empty collection - in the case of a tie, return one of the winners
Thanks to this site for the challenge idea where it is considered Very Hard level in JavaScript.
Please submit your solutions as comments on this gist.
Note: Some of the answers won't work with mixed types when sorted, because of class cast exception.
For example:
[nil "hello" true nil]