Jan Dvorak's answer is probably best:
- Start with two empty candidate slots and two counters set to 0.
- for each item:
- if it is equal to either candidate, increment the corresponding count
- else if there is an empty slot (i.e. a slot with count 0), put it in that slot and set the count to 1
- else reduce both counters by 1
At the end, make a second pass over the array to check whether the candidates really do have the required count. This isn't allowed by the question you link to but I don't see how to avoid it for this modified version. If there is a value that occurs more than n/3 times then it will be in a slot, but you don't know which one it is.
If this modified version of the question guaranteed that there were two values with more than n/3 elements (in general, k-1 values with more than n/k) then we wouldn't need the second pass. But when the original question has k=2 and 1 guaranteed majority there's no way to know whether we "should" generalize it as guaranteeing 1 such element or guaranteeing k-1. The stronger the guarantee, the easier the problem.
answered 2013-02-19T11:14:41.740