-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountElementsWithMaximumFrequency.ts
56 lines (51 loc) · 1.71 KB
/
CountElementsWithMaximumFrequency.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Source : https://leetcode.com/problems/count-elements-with-maximum-frequency/
// Author : francisco
// Date : 2024-01-14
/*****************************************************************************************************
*
* You are given an array nums consisting of positive integers.
*
* Return the total frequencies of elements in nums such that those elements all have the maximum
* frequency.
*
* The frequency of an element is the number of occurrences of that element in the array.
*
* Example 1:
*
* Input: nums = [1,2,2,3,1,4]
* Output: 4
* Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
* So the number of elements in the array with maximum frequency is 4.
*
* Example 2:
*
* Input: nums = [1,2,3,4,5]
* Output: 5
* Explanation: All elements of the array have a frequency of 1 which is the maximum.
* So the number of elements in the array with maximum frequency is 5.
*
* Constraints:
*
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 100
******************************************************************************************************/
/**
* @param {number[]} nums
* @returns {number}
* return the total frequencies of elements in nums such that those elements have the maximum frequency
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
export function maxFrequencyElements(nums: number[]): number {
const freq: Record<number, number> = {};
let max: number = 0;
for (const num of nums) {
const val: number = (freq[num] ?? 0) + 1;
freq[num] = val;
if (val > max) max = val;
}
return Object.values(freq).reduce((acc: number, crr: number) => {
if (crr === max) return acc + crr;
return acc;
}, 0);
}