0% completed
Given an array of positive integers named nums
, return the total frequencies of the elements in nums
that have a maximum frequency.
The frequency of an element is defined as the number of times that element is repeated in the array.
Example 1:
[3, 2, 2, 3, 1, 4]
Example 2:
[5, 5, 5, 2, 2, 3]
Example 3:
[7, 8, 8, 7, 9, 9]
Constraints:
To solve this problem, the approach involves counting the occurrences of each number in the array nums. We can achieve this by using a hash map (or dictionary) to store the frequency of each element. Once we have the frequencies, we can determine the highest frequency. Finally, we sum the frequencies of all elements that have this highest frequency.
This approach allows you to efficiently count and compare frequencies in a single pass through the array, followed by a second pass through the frequency counts to compute the result.
Initialize a hash map (frequencyMap) to store the frequency of each element in the array nums.
Calculate the frequency of array elements.
Find the maximum frequency from the values in the frequency map.
Sum the frequencies of elements that have the maximum frequency.
Return the total frequency of elements that have the maximum frequency (totalMaxFrequency).
Let's consider the Input: nums = [3, 2, 2, 3, 1, 4]
Initialize frequencyMap: Create an empty hash map to store frequencies.
Iterate through nums:
Find the maximum frequency:
Sum the frequencies of elements with maximum frequency:
Return totalMaxFrequency:
In this one-pass solution, we can use a hash map to count the frequency of each element in the array nums. We will also keep track of the maximum frequency encountered. As we iterate through the array, we update the frequency map and compare the current frequency to the maximum frequency. If a new maximum frequency is found, we reset the total frequencies to this new maximum frequency. If the current frequency matches the maximum frequency, we add it to the total frequencies. This approach ensures that we accurately count the total frequencies of elements with the highest frequency.
frequencies
to store the frequency of each element.maxFrequency
and totalFrequencies
to 0.maxFrequency
, update maxFrequency
and reset totalFrequencies
to the current frequency.maxFrequency
, add the current frequency to totalFrequencies
.totalFrequencies
.Let's use the first example: [3, 2, 2, 3, 1, 4]
.
Initialize Data Structures:
frequencies
(hash map): {}
maxFrequency
: 0
totalFrequencies
: 0
Process First Element (3
):
frequencies
: {3: 1}
frequency
of 3
: 1
1
> maxFrequency
(0
):
maxFrequency
: 1
totalFrequencies
: 1
frequencies = {3: 1}
, maxFrequency = 1
, totalFrequencies = 1
Process Second Element (2
):
frequencies
: {3: 1, 2: 1}
frequency
of 2
: 1
1
== maxFrequency
(1
):
totalFrequencies
: 1 + 1 = 2
frequencies = {3: 1, 2: 1}
, maxFrequency = 1
, totalFrequencies = 2
Process Third Element (2
):
frequencies
: {3: 1, 2: 2}
frequency
of 2
: 2
2
> maxFrequency
(1
):
maxFrequency
: 2
totalFrequencies
: 2
frequencies = {3: 1, 2: 2}
, maxFrequency = 2
, totalFrequencies = 2
Process Fourth Element (3
):
frequencies
: {3: 2, 2: 2}
frequency
of 3
: 2
2
== maxFrequency
(2
):
totalFrequencies
: 2 + 2 = 4
frequencies = {3: 2, 2: 2}
, maxFrequency = 2
, totalFrequencies = 4
Process Fifth Element (1
):
frequencies
: {3: 2, 2: 2, 1: 1}
frequency
of 1
: 1
1
< maxFrequency
(2
), no changes to maxFrequency
or totalFrequencies
.frequencies = {3: 2, 2: 2, 1: 1}
, maxFrequency = 2
, totalFrequencies = 4
Process Sixth Element (4
):
frequencies
: {3: 2, 2: 2, 1: 1, 4: 1}
frequency
of 4
: 1
1
< maxFrequency
(2
), no changes to maxFrequency
or totalFrequencies
.frequencies = {3: 2, 2: 2, 1: 1, 4: 1}
, maxFrequency = 2
, totalFrequencies = 4
Final Output:
totalFrequencies
: 4