Make Set.count O(1)
Seems easy to track items when they are added and removed
Jack Fox commented
You are going to incur the cost somewhere. It's usually preferable to incur it when you request count.
Robert Pickering commented
Sadly, not as straight forward as it sounds, since set also support filter and partition as well as add. filter and partition work directly on the SetTree type that represents set internally, so these operation would need to return the new count. It could be done, but the question is should it be done, as the extra over heads may out weight the perf gains from have Set.count O(1). Difficult call to make imho.