Find if there is an element repeating itself n/k times
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The problem of finding an element that appears more than n / k times is a useful generalization of the majority-element problem. When k = 2, you are looking for an element that appears more than half the time. For larger k, there can be multiple valid answers, but never too many, and that observation drives the efficient solution.
The Key Observation
In an array of length n, there can be at most k - 1 elements that appear more than n / k times. If there were k such elements, the total number of occurrences would exceed n, which is impossible.
That means you do not need to track every distinct value. You only need to track up to k - 1 candidate elements and then verify them.
This is the generalized Boyer-Moore voting idea.
The Simple Hash Map Solution
If memory is not a concern, the easiest implementation is a frequency map:
This runs in O(n) time and O(n) space in the worst case. It is often the best answer in production code when clarity matters more than minimizing auxiliary memory.
The O(k) Space Approach
To reduce space, keep at most k - 1 candidate counters. The algorithm works in two passes:
- find possible candidates
- count them again to verify which ones really exceed
n / k
The cancellation step is the important idea. When the candidate set is full and a new different value appears, you reduce every candidate count by one. Conceptually, that removes one occurrence from k different elements, which cannot eliminate a truly frequent element permanently.
Why Verification Is Required
After the first pass, the tracked values are only candidates. They survived repeated cancellations, but they are not yet guaranteed to appear more than n / k times.
For example, a value can remain in the candidate map simply because the distribution of other values happened to cancel differently. That is why the second counting pass is mandatory.
This is the same pattern as the ordinary majority-element voting algorithm: candidate selection first, proof by recount second.
When to Use Which Method
Use the hash map method when:
- the array fits comfortably in memory
- readability matters most
- '
kis not especially small relative ton'
Use the optimized method when:
- you want
O(k)extra space - you are solving an interview-style problem
- the input is large and the frequency map would be wasteful
For many real applications, the hash map version is easier to maintain. The optimized algorithm is elegant, but it is easier to get wrong.
Common Pitfalls
The biggest mistake is returning candidates immediately after the first pass. Those values must be verified.
Another mistake is misreading the condition. The problem asks for elements appearing more than n / k times, not greater than or equal to n / k.
People also forget that there can be multiple valid answers. This is not always a single-element problem.
Finally, validate k. If k <= 1, the threshold is not meaningful for this algorithm and the function should reject the input.
Summary
- The
n / krepetition problem generalizes the majority-element problem. - At most
k - 1elements can appear more thann / ktimes. - A hash map gives a clear
O(n)time solution with higher space usage. - The generalized Boyer-Moore approach reduces extra space to
O(k). - Candidate verification in a second pass is essential for correctness.

