Foundations Counting with arrays and maps
45. Sum of Highest and Lowest Frequency
You are given an array of integers nums. The frequency of a value is the number of times it appears in
nums.
Look at the frequencies of the distinct values in nums. Return the highest of them plus the lowest of them.
Only values that appear in nums have a frequency, so the lowest frequency is at least 1. When every value
appears the same number of times, the highest and the lowest frequency are the same number, and the answer
counts it twice.
Example 1
Input: nums = [4, -2, 4, 7, -2, 4, 9]
4Explanation: 4 appears 3 times, −2 twice, and 7 and 9 once each. The highest frequency is 3 and the lowest is 1, so the answer is 3 + 1 = 4.
Example 2
Input: nums = [-1000000000, -1000000000, -1000000000]
6Explanation: There is one distinct value, and it appears 3 times. Its frequency is both the highest and the lowest, so the answer is 3 + 3 = 6.
Example 3
Input: nums = [8, 0, 3, 3, 8, 0]
4Explanation: Each of 8, 0 and 3 appears twice, so the answer is 2 + 2 = 4.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 105-109 ≤ nums[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.