当前位置:网站首页>Daily 3 questions (2) - find out the lucky numbers in the array
Daily 3 questions (2) - find out the lucky numbers in the array
2022-06-25 11:03:00 【Programmed ape without hair loss 2】
subject :
In an array of integers , If the frequency of an integer is the same as its value , We call this whole number 「 Lucky number 」.
Give you an array of integers arr, Please find out and return a lucky number .
If there are more than one lucky number in the array , Just go back Maximum the .
If there are no lucky numbers in the array , Then return to -1 .
Example 1:
Input :arr = [2,2,3,4]
Output :2
explain : The only lucky number in the array is 2 , Because the value 2 The frequency of its occurrence is also 2 .
Example 2:
Input :arr = [1,2,2,3,3,3]
Output :3
explain :1、2 as well as 3 It's all lucky numbers , Just go back to the largest of them 3 .
Example 3:
Input :arr = [2,2,2,3,3]
Output :-1
explain : There is no lucky number in the array .
Example 4:
Input :arr = [5]
Output :-1
Example 5:
Input :arr = [7,7,7,7,7,7,7]
Output :7
Tips :
1 <= arr.length <= 500
1 <= arr[i] <= 500
Ideas :
hash mapping
java Code :
class Solution {
public int findLucky(int[] arr) {
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for (int x : arr) {
m.put(x, m.getOrDefault(x, 0) + 1);
}
int ans = -1;
for (Map.Entry<Integer, Integer> entry : m.entrySet()) {
int key = entry.getKey(), value = entry.getValue();
if (key == value) {
ans = Math.max(ans, key);
}
}
return ans;
}
}
边栏推荐
- Handling of NPM I installation problems
- Server rendering
- 今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
- Network protocol learning -- lldp protocol learning
- 【论文阅读|深读】DRNE:Deep Recursive Network Embedding with Regular Equivalence
- Dell technology performs the "fast" formula and plays ci/cd
- Multiple environment variables
- Android之Kotlin语法详解与使用
- [file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?
- Android:kotlin中Gson与JSON的泛型映射解析
猜你喜欢

XSS attack

Binder explanation of Android interview notes

Use of Siemens plcs7-200 (I) -- Introduction to development environment and configuration software

视频会议一体机的技术实践和发展趋势

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7

报名开启|飞桨黑客马拉松第三期如约而至,久等啦

NuxtJS实战案例

OpenCV学习(二)---树莓派上安装opencv

Use of three-level linkage plug-ins selected by provinces and cities

1-7Vmware中的快照与克隆
随机推荐
性能之网络篇
好好思考
Kotlin implements a simple login page
Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
Array structure collation
Cdn+cos ultra detailed steps for drawing bed construction
Oracle彻底卸载的完整步骤
中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析
无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》
网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目
1-7Vmware中的快照与克隆
Nuxtjs actual combat case
每日3題(3)-檢查整數及其兩倍數是否存在
Google Earth engine (GEE) - evaluate enables one click batch download of all single images in the research area (some areas in Shanghai)
Remove the problem of orange border on the desktop control in WebView
A random number generator
新学派:不诈骗经济学
Handler asynchronous message processing
【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?
垃圾回收机制