{"id":10104,"date":"2024-01-15T17:56:31","date_gmt":"2024-01-16T01:56:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10104"},"modified":"2024-01-15T17:58:47","modified_gmt":"2024-01-16T01:58:47","slug":"leetcode-3005-count-elements-with-maximum-frequency","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-3005-count-elements-with-maximum-frequency\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 3005. Count Elements With Maximum Frequency"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>nums<\/code>&nbsp;consisting of&nbsp;<strong>positive<\/strong>&nbsp;integers.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>total frequencies<\/strong>&nbsp;of elements in<\/em><em>&nbsp;<\/em><code>nums<\/code>&nbsp;<em>such that those elements all have the&nbsp;<strong>maximum<\/strong>&nbsp;frequency<\/em>.<\/p>\n\n\n\n<p>The&nbsp;<strong>frequency<\/strong>&nbsp;of an element is the number of occurrences of that element in the array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,2,3,1,4]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.\nSo the number of elements in the array with maximum frequency is 4.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> All elements of the array have a frequency of 1 which is the maximum.\nSo the number of elements in the array with maximum frequency is 5.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to store the frequency of each element, and compare it with a running maximum frequency. Reset answer if current frequency is greater than maximum frequency. Increment the answer if current frequency is equal to the maximum frequency.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n\/\/ Author: Huahua\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int maxFrequencyElements(vector<int>& nums) {\n    unordered_map<int, int> m;\n    int freq = 0;\n    int ans = 0;\n    for (int x : nums) {\n      if (++m[x] > freq)\n        freq = m[x], ans = 0;\n      if (m[x] == freq)\n        ans += m[x];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;nums&nbsp;consisting of&nbsp;positive&nbsp;integers. Return&nbsp;the&nbsp;total frequencies&nbsp;of elements in&nbsp;nums&nbsp;such that those elements all have the&nbsp;maximum&nbsp;frequency. The&nbsp;frequency&nbsp;of an element is the number of occurrences of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,24,82],"class_list":["post-10104","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-frequency","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10104"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10104\/revisions"}],"predecessor-version":[{"id":10108,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10104\/revisions\/10108"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}