Ruby Ruby - 3 years ago 185
Scala Question

How do I efficiently count distinct fields in a collection?

I am currently doing this:

val count =

creates an intermediary collection, which in my case is a Vector thousands of times larger than what

How do I bypass this intermediate step and get the set of distinct shapes? Or, even better, the count of distinct shapes.

Answer Source

You can use an iterator to not create the intermediate collection and then accrue the shapes in a Set to get the distinct ones:

val count =

Alternatively, you can use collection.breakOut to accrue the items in a Set without creating the intermediate collection (another answer suggested using breakOut, but in a different way):

val distinctShapes: Set[Shape] =
val count = distinctShapes.size
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download