|
| 1 | +<h1 align='center'>Count - Elements - With - Maximum - Frequency</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Problem Explanation |
| 10 | +The problem is asking us to **count how many elements** appear with the **maximum frequency** in a given list of integers. |
| 11 | + |
| 12 | +### Problem Breakdown: |
| 13 | +- You are given a vector of integers, `nums`. |
| 14 | +- The task is to determine the element(s) that appear the most frequently (i.e., the maximum frequency). |
| 15 | +- Once you have the most frequent element(s), return the total number of times the maximum frequency occurs, **counting all such elements** with the maximum frequency. |
| 16 | + |
| 17 | +### Example 1: |
| 18 | +```cpp |
| 19 | +nums = [1, 2, 2, 3, 3, 3, 4] |
| 20 | +``` |
| 21 | +- Frequency of elements: |
| 22 | + - `1` appears 1 time. |
| 23 | + - `2` appears 2 times. |
| 24 | + - `3` appears 3 times. |
| 25 | + - `4` appears 1 time. |
| 26 | + |
| 27 | +- The maximum frequency is `3` (for element `3`). |
| 28 | +- There is 1 element (`3`) with this frequency. |
| 29 | + |
| 30 | +**Output:** `1` (because only the element `3` has the maximum frequency) |
| 31 | + |
| 32 | +### Example 2: |
| 33 | +```cpp |
| 34 | +nums = [1, 2, 2, 3, 3] |
| 35 | +``` |
| 36 | +- Frequency of elements: |
| 37 | + - `1` appears 1 time. |
| 38 | + - `2` appears 2 times. |
| 39 | + - `3` appears 2 times. |
| 40 | + |
| 41 | +- The maximum frequency is `2` (for elements `2` and `3`). |
| 42 | +- There are 2 elements (`2` and `3`) with this frequency. |
| 43 | + |
| 44 | +**Output:** `4` (because both `2` and `3` appear 2 times, so the total frequency count is `2 + 2 = 4`) |
| 45 | + |
| 46 | +### Approach Explanation: |
| 47 | + |
| 48 | +1. **Count the Frequency of Each Element:** |
| 49 | + - The first step is to calculate how many times each element occurs in the list. We can use a `unordered_map` (hash map) where: |
| 50 | + - The key is the element. |
| 51 | + - The value is the frequency of that element. |
| 52 | + |
| 53 | +2. **Find the Maximum Frequency:** |
| 54 | + - Once we know how many times each element appears, we need to find the maximum frequency from all elements. |
| 55 | + |
| 56 | +3. **Count How Many Elements Have the Maximum Frequency:** |
| 57 | + - After identifying the maximum frequency, count how many different elements share this maximum frequency. |
| 58 | + |
| 59 | +### Detailed Approach with Example: |
| 60 | + |
| 61 | +Given `nums = [1, 2, 2, 3, 3, 3, 4]`, follow these steps: |
| 62 | + |
| 63 | +- **Step 1:** Create a frequency map: |
| 64 | + ``` |
| 65 | + freqMap = {1: 1, 2: 2, 3: 3, 4: 1} |
| 66 | + ``` |
| 67 | +- **Step 2:** Find the maximum frequency: |
| 68 | + ``` |
| 69 | + maxFreq = 3 |
| 70 | + ``` |
| 71 | +- **Step 3:** Count how many elements have the maximum frequency: |
| 72 | + ``` |
| 73 | + totalCount = 1 (Only element `3` appears 3 times) |
| 74 | + ``` |
| 75 | + |
| 76 | +## Problem Solution |
| 77 | +```cpp |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + int maxFrequencyElements(vector<int>& nums) { |
| 81 | + unordered_map<int, int> freqMap; |
| 82 | + |
| 83 | + for(auto i : nums) freqMap[i]++; |
| 84 | + |
| 85 | + int maxFreq = 0; |
| 86 | + |
| 87 | + for(auto pair : freqMap) maxFreq = max(maxFreq, pair.second); |
| 88 | + |
| 89 | + int totalCount = 0; |
| 90 | + for (auto pair : freqMap) if (pair.second == maxFreq) totalCount += pair.second; |
| 91 | + |
| 92 | + return totalCount; |
| 93 | + } |
| 94 | +}; |
| 95 | +``` |
| 96 | + |
| 97 | +## Problem Solution Explanation |
| 98 | + |
| 99 | +```cpp |
| 100 | +class Solution { |
| 101 | +public: |
| 102 | + int maxFrequencyElements(vector<int>& nums) { |
| 103 | + unordered_map<int, int> freqMap; |
| 104 | +``` |
| 105 | +- This line initializes an `unordered_map` called `freqMap`. The key is an integer (`int`), and the value is an integer representing how many times that key appears in the list. |
| 106 | +
|
| 107 | +```cpp |
| 108 | + for(auto i : nums) freqMap[i]++; |
| 109 | +``` |
| 110 | +- This loop iterates through each element in the `nums` vector. |
| 111 | +- For each element `i`, we increment its corresponding frequency in the `freqMap`. |
| 112 | + |
| 113 | +```cpp |
| 114 | + int maxFreq = 0; |
| 115 | +``` |
| 116 | +- This initializes a variable `maxFreq` that will hold the highest frequency of any element in the `nums` vector. |
| 117 | + |
| 118 | +```cpp |
| 119 | + for(auto pair : freqMap) maxFreq = max(maxFreq, pair.second); |
| 120 | +``` |
| 121 | +- This loop iterates through each key-value pair in `freqMap`. |
| 122 | +- For each pair, we update `maxFreq` to the larger value between the current `maxFreq` and the frequency (`pair.second`). |
| 123 | + |
| 124 | +```cpp |
| 125 | + int totalCount = 0; |
| 126 | + for (auto pair : freqMap) if (pair.second == maxFreq) totalCount += pair.second; |
| 127 | +``` |
| 128 | +- We initialize a variable `totalCount` to 0. |
| 129 | +- Then, we iterate through each pair in `freqMap` and check if the frequency (`pair.second`) equals `maxFreq`. If it does, we add the frequency to `totalCount`. |
| 130 | + |
| 131 | +```cpp |
| 132 | + return totalCount; |
| 133 | + } |
| 134 | +}; |
| 135 | +``` |
| 136 | +- Finally, we return the total count of all elements that have the maximum frequency. |
| 137 | + |
| 138 | +### Step 3: Examples and Explanation |
| 139 | + |
| 140 | +Let’s walk through the examples with the code. |
| 141 | + |
| 142 | +#### Example 1: `nums = [1, 2, 2, 3, 3, 3, 4]` |
| 143 | + |
| 144 | +1. **Frequency Map Creation:** |
| 145 | + ``` |
| 146 | + freqMap = {1: 1, 2: 2, 3: 3, 4: 1} |
| 147 | + ``` |
| 148 | + |
| 149 | +2. **Finding the Maximum Frequency:** |
| 150 | + ``` |
| 151 | + maxFreq = 3 (The maximum frequency is for element 3) |
| 152 | + ``` |
| 153 | + |
| 154 | +3. **Count Elements with Maximum Frequency:** |
| 155 | + ``` |
| 156 | + totalCount = 3 (Only element `3` has the maximum frequency) |
| 157 | + ``` |
| 158 | + |
| 159 | +**Output:** `3` |
| 160 | + |
| 161 | +#### Example 2: `nums = [1, 2, 2, 3, 3]` |
| 162 | + |
| 163 | +1. **Frequency Map Creation:** |
| 164 | + ``` |
| 165 | + freqMap = {1: 1, 2: 2, 3: 2} |
| 166 | + ``` |
| 167 | + |
| 168 | +2. **Finding the Maximum Frequency:** |
| 169 | + ``` |
| 170 | + maxFreq = 2 (The maximum frequency is 2) |
| 171 | + ``` |
| 172 | + |
| 173 | +3. **Count Elements with Maximum Frequency:** |
| 174 | + ``` |
| 175 | + totalCount = 4 (Both `2` and `3` have the maximum frequency of 2) |
| 176 | + ``` |
| 177 | + |
| 178 | +**Output:** `4` |
| 179 | + |
| 180 | +### Step 4: Time and Space Complexity |
| 181 | + |
| 182 | +#### Time Complexity: |
| 183 | +- **Building the Frequency Map:** |
| 184 | + - We loop through each element in `nums` to build the frequency map, which takes **O(n)** time, where `n` is the number of elements in `nums`. |
| 185 | + |
| 186 | +- **Finding Maximum Frequency:** |
| 187 | + - We loop through each key-value pair in the map to find the maximum frequency, which takes **O(k)** time, where `k` is the number of distinct elements in the map. |
| 188 | + |
| 189 | +- **Counting Elements with Maximum Frequency:** |
| 190 | + - We loop through each key-value pair again to count the elements that have the maximum frequency, which also takes **O(k)** time. |
| 191 | + |
| 192 | +**Overall Time Complexity:** O(n + k), where `n` is the number of elements in `nums` and `k` is the number of distinct elements. |
| 193 | + |
| 194 | +In the worst case, `k` can be equal to `n`, so the time complexity is **O(n)**. |
| 195 | + |
| 196 | +#### Space Complexity: |
| 197 | +- We use an `unordered_map` to store the frequency of each element, which requires **O(k)** space, where `k` is the number of distinct elements in the input array. |
| 198 | + |
| 199 | +**Overall Space Complexity:** O(k), where `k` is the number of distinct elements. |
| 200 | + |
| 201 | +### Step 5: Recommendations for Students |
| 202 | + |
| 203 | +- **Practice with Different Inputs:** |
| 204 | + - Try using an empty array `nums = []`, or an array where all elements are the same, e.g., `nums = [1, 1, 1, 1]`. |
| 205 | + |
| 206 | +- **Handle Edge Cases:** |
| 207 | + - Consider cases where the array is empty or all elements have the same frequency. |
| 208 | + |
| 209 | +- **Understand Hash Maps:** |
| 210 | + - Hash maps are efficient for counting frequencies because they provide average O(1) time complexity for both insertions and lookups. |
| 211 | + |
| 212 | +- **Consider Alternative Approaches:** |
| 213 | + - While the `unordered_map` is efficient, consider how you would solve this problem if you were constrained to using a different data structure. |
0 commit comments