{"id":6546,"date":"2020-03-28T21:48:39","date_gmt":"2020-03-29T04:48:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6546"},"modified":"2020-03-28T21:49:10","modified_gmt":"2020-03-29T04:49:10","slug":"leetcode-1394-find-lucky-integer-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1394-find-lucky-integer-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1394. Find Lucky Integer in an Array"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>arr<\/code>, a lucky integer is an integer which has a frequency in the array equal to its value.<\/p>\n\n\n\n<p>Return&nbsp;<em>a lucky integer<\/em>&nbsp;in the array. If there are multiple lucky integers return the&nbsp;<strong>largest<\/strong>&nbsp;of them. If there is no lucky&nbsp;integer return&nbsp;<strong>-1<\/strong>.<\/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> arr = [2,2,3,4]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The only lucky number in the array is 2 because frequency[2] == 2.\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> arr = [1,2,2,3,3,3]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> 1, 2 and 3 are all lucky numbers, return the largest of them.\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> arr = [2,2,2,3,3]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There are no lucky numbers in the array.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [5]\n<strong>Output:<\/strong> -1\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [7,7,7,7,7,7,7]\n<strong>Output:<\/strong> 7\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 500<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 500<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\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 findLucky(vector<int>& arr) {\n    unordered_map<int, int> freq;\n    for (int x : arr) ++freq[x];\n    int ans = -1;\n    for (const auto& [key, count] : freq)\n      if (key == count) ans = max(ans, key);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr, a lucky integer is an integer which has a frequency in the array equal to its value. Return&nbsp;a lucky integer&nbsp;in&#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-6546","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\/6546","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=6546"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6546\/revisions"}],"predecessor-version":[{"id":6548,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6546\/revisions\/6548"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}