Foundations Counting with arrays and maps

45. Sum of Highest and Lowest Frequency

Medium

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]

Output: 4

Explanation: 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]

Output: 6

Explanation: 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]

Output: 4

Explanation: 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.

nums = [6, 6, 1, 6, 1, 5]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.