Count once, then ask
transcript
A list holds many repeated values. Some appear once, some appear often. The rule asks how often each value appears. Checking each value against the list is slow. Count once, then ask the question of the counts. Here is how.
Some rules need a count of exactly one. A unique code is one example. Some need more than half of the list. That is a majority. Some spend a removal budget. They pay for the smallest counts first.
Five values have the counts one, one, two, three and four. The budget is four entries. Pay for the first two values. That costs two entries, and two values are gone. Pay for the value with count two. The budget is now empty, and three values are gone. Two values stay, with counts three and four. Cheapest first leaves the fewest values.
A strict comparison is a common slip. Say the budget is one and a count is one. Strict code keeps the value. But one entry costs exactly one, so it can go. Use greater or equal. Then the value goes and the budget reaches zero.
Count in one pass, and put the list aside. The map holds every count. Write the rule as one line about the counts before you code. For a budget or a rank, sort the counts themselves.
Count once in a single pass. The map is enough. Write the rule on the counts, not on the items. Spend a budget from the smallest count. Use greater or equal. Full breakdown in the lecture.