{"id":9804,"date":"2022-09-11T07:54:39","date_gmt":"2022-09-11T14:54:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9804"},"modified":"2022-09-11T07:54:59","modified_gmt":"2022-09-11T14:54:59","slug":"leetcode-2404-most-frequent-even-element%ef%bf%bc","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2404-most-frequent-even-element%ef%bf%bc\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2404.\u00a0Most Frequent Even Element"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>, return&nbsp;<em>the most frequent even element<\/em>.<\/p>\n\n\n\n<p>If there is a tie, return the&nbsp;<strong>smallest<\/strong>&nbsp;one. If there is no such element, return&nbsp;<code>-1<\/code>.<\/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 = [0,1,2,2,4,4,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nThe even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.\nWe return the smallest one, which is 2.<\/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 = [4,4,4,9,2,4]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> 4 is the even element appears the most.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [29,47,21,41,13,37,25,7]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There is no even element.\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;= 2000<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/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 even numbers.<\/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<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int mostFrequentEven(vector<int>& nums) {\n    unordered_map<int, int> m;\n    int ans = -1;\n    int best = 0;\n    for (int x : nums) {\n      if (x & 1) continue;\n      int cur = ++m[x];\n      if (cur > best) {\n        best = cur;\n        ans = x;\n      } else if (cur == best && x < ans) {\n        ans = x;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums, return&nbsp;the most frequent even element. If there is a tie, return the&nbsp;smallest&nbsp;one. If there is no such element, return&nbsp;-1. Example 1:&#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,387,82],"class_list":["post-9804","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-freq","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9804","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=9804"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9804\/revisions"}],"predecessor-version":[{"id":9806,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9804\/revisions\/9806"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}