You're looking for an item with a rep-count that's non-zero (mod 3). I think I'd do it recursively:
- split the array in half
- find items with rep count that's non-zero (mod 3) in each half
- merge the halves, keeping counts for unequal keys, and adding the counts for equal keys
- strike out any with count = 0 (mod 3)
- return that as the set of values for the current part of the input.
Without even trying to optimize things beyond the basic algorithm (e.g., not worrying about storing only two bits per count), this seems to do pretty well. I've included code to generate a reasonably large test case (e.g., 1500+ items) and print out the sizes of the maps it's creating. At any given time, it seems to have a maximum of around 50 items in the maps it creates (i.e., it only uses two maps at a time, and the largest I've seen is around 25 items). Technically, as it stands I believe this is currently something like O(N log N), but if you switched to a hash-based container, I believe you'd expect O(N).
#include