The performance issues seem reasonably obvious: you're doing a
count
(which one would assume is O(n)) on each iteration and you're also doing a remove
each iteration (which is also O(n)). So, you're doing O(n^2) operations, given that map
will also be O(n). You can at least figure out the most common in a single pass over the array (it is left as an exercise to figure out exactly how!) and then do the summing after that.