当前位置:网站首页>Find the number with the most occurrences in the array
Find the number with the most occurrences in the array
2022-07-24 06:10:00 【A little cute C】
Use HashMap, Every Entry Of key Store the numbers in the array ,value Store the number of occurrences of this number , First, traverse the array element construction HashMap, And then I'm going to go through each one Entry, Find out the maximum value Corresponding key, That is, the number with the most occurrences . The time complexity of this algorithm is O(n).Java The code is as follows
public class SearchMuch {
public static void candidate (int[] array) // Find the number that appears the most in the array
{
// map Of key Store the numbers in the array ,value Store the number of occurrences of this number
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 0; i < array.length; i++)
{
if(map.containsKey(array[i]))
{
int formerValue = map.get(array[i]);
map.put(array[i], formerValue + 1); // The number of times this number appears plus 1
}
else
{
map.put(array[i], 1); // This number appears for the first time
}
}
Collection<Integer> count = map.values();
// find map Of value The maximum , That is, the number of times that the most numbers appear in the array
int maxCount = Collections.max(count);
int maxNumber = 0;
for(Map.Entry<Integer, Integer> entry : map.entrySet())
{
// obtain value by maxCount Of key, That is, the number that appears the most times in the array
if(entry.getValue() == maxCount)
{
maxNumber = entry.getKey();
}
}
System.out.println(" The number with the most occurrences is :" + maxNumber);
System.out.println(" The number appears in total " + maxCount + " Time ");
}
}边栏推荐
- Bat batch script, running multiple files at the same time, batch commands executed in sequence, and xshell script.
- day1-jvm+leetcode
- Better CV link collection (dynamic update)
- JUC并发编程基础(7)--多线程锁
- 通道注意力与空间注意力模块
- HAL_Delay()延时误差约1ms的问题
- On the concepts of "input channel" and "output channel" in convolutional neural networks
- [MYCAT] MYCAT configuration file
- JUC concurrent programming foundation (9) -- thread pool
- How to solve the problem of large distribution gap between training set and test set
猜你喜欢
![[activiti] personal task](/img/bc/80ac4067f6c58785acb4273f9507ee.png)
[activiti] personal task

Yolov5 learning summary (continuously updated)

【深度学习】手写神经网络模型保存

JUC并发编程基础(8)--读写锁

Foundation of JUC concurrent programming (7) -- multithread lock

MySQL基础---约束

JUC并发编程基础(6)--Lock锁

Thymeleaf quick start learning

day1-jvm+leetcode

Answers and analysis of some after-school exercises in signals and systems (Wujing)
随机推荐
Raspberry pie is of great use. Use the campus network to build a campus local website
JDBC进阶—— 师承尚硅谷(DAO)
JSON. Dumps() function parsing
JDBC初级学习 ------(师承尚硅谷)
String methods and instances
Machine learning (Zhou Zhihua) Chapter 3 Notes on learning linear models
通道注意力与空间注意力模块
JUC并发编程基础(7)--多线程锁
Accurate calculation of time delay detailed explanation of VxWorks timestamp
KMP代码分布详解
In GCC__ attribute__ ((constructor) and__ attribute__ ((destructor)).
Unity基础知识及一些基本API的使用
【深度学习】手把手教你写“手写数字识别神经网络“,不使用任何框架,纯Numpy
Headlong platform operation
AD1256
Day2 websocket+ sort
JVM system learning
Foundation of JUC concurrent programming (7) -- multithread lock
CRC-16 MODBUS code
Machine learning (zhouzhihua) Chapter 2 model selection and evaluation notes learning experience