Sum of Highest and Lowest Frequency
Pattern: Hashing basics
Count every value, then add the largest count and the smallest count.
The problem
You are given an array nums of between 1 and 100,000 whole numbers. Look at how many times each distinct value appears.
Return the largest of those counts plus the smallest. For example, in [1, 1, 1, 2, 3, 3] the counts are 3, 1 and 2, so
the answer is 3 + 1 = 4. If all values are the same, the largest and smallest count are the same number.
On the original site the problem is written a little differently, so use the link above for its exact wording. The
function name sumHighestAndLowestFrequency and the limits are this guide's own.
Think first
- The answer uses counts, not values. Which value had the highest count does not matter at all. What do you need?
- What should the smallest count start at, so that the first real count always replaces it?
- What is the answer for
[5, 5, 5, 5]?
Hints
Brute force, and why it fails
For each distinct value, scan the whole array to count it, then compare. That is O(n²) and too slow for 100,000 elements. Sorting and measuring runs of equal values is O(n log n). A hash map counts everything in O(n).
The key insight
Once the counts exist, the problem forgets the values entirely: it is just "largest plus smallest" over a list of counts, one scan.
Dry run
| count | {1: 3, 2: 1, 3: 2} |
|---|
nums = [1, 1, 1, 2, 3, 3]. After the counting pass, 1 appears 3 times, 2 once and 3 twice.
The plan
count = map from value to how many times it appears
highest = 0
lowest = largest possible integer
for times in the counts:
highest = max(highest, times)
lowest = min(lowest, times)
return highest + lowest
Complexity
- Time
O(n)- Space
O(k)
One pass to count and one pass over the k distinct values. The map holds k entries.
Edge cases
- One element: its count is 1 for both, so the answer is 2.
- All equal, such as
[5, 5, 5, 5]: 4 + 4 = 8. - All distinct: every count is 1, so the answer is 2.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int sumHighestAndLowestFrequency(int[] nums) {
Map<Integer, Integer> count = new HashMap<>();
for (int x : nums) {
count.put(x, count.getOrDefault(x, 0) + 1);
}
int highest = 0;
int lowest = Integer.MAX_VALUE;
for (int times : count.values()) {
highest = Math.max(highest, times);
lowest = Math.min(lowest, times);
}
return highest + lowest;
}
}C++
class Solution {
public:
int sumHighestAndLowestFrequency(vector<int>& nums) {
unordered_map<int, int> count;
for (int x : nums) {
count[x]++;
}
int highest = 0;
int lowest = INT_MAX;
for (auto& entry : count) {
highest = max(highest, entry.second);
lowest = min(lowest, entry.second);
}
return highest + lowest;
}
};Python
class Solution:
def sumHighestAndLowestFrequency(self, nums):
count = {}
for x in nums:
count[x] = count.get(x, 0) + 1
highest = max(count.values())
lowest = min(count.values())
return highest + lowestCommon mistakes
- Starting
lowestat 0, which is never beaten by a real count. - Adding the values instead of their counts.
- Counting with an array indexed by value, which fails for negative or very large values.