当前位置:网站首页>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 ");
}
}边栏推荐
- 【树莓派4B】七、远程登录树莓派的方法总结XShell,PuTTY,vncServer,Xrdp
- String methods and instances
- vsual studio 2013环境 Udp组播
- 使用Keras实现 基于注意力机制(Attention)的 LSTM 时间序列预测
- [MYCAT] Introduction to MYCAT
- JUC并发编程基础(1)--相关基础概念
- Conversion of world coordinate system, camera coordinate system and image coordinate system
- [USB host] stm32h7 cubemx porting USB host with FreeRTOS to read USB disk, usbh_ Process_ The OS is stuck. There is a value of 0xa5a5a5
- day5-jvm
- JUC并发编程基础(7)--多线程锁
猜你喜欢

Common features of ES6
![[activiti] process example](/img/77/63d7d1ea8c634fb3741383f40f38e7.png)
[activiti] process example

Channel attention and spatial attention module

Conversion of world coordinate system, camera coordinate system and image coordinate system

Jestson installs IBus input method

Machine learning (zhouzhihua) Chapter 2 model selection and evaluation notes learning experience

How to solve the problem of large distribution gap between training set and test set

Thymeleaf quick start learning

使用Keras和LSTM实现对于长期趋势记忆的时间序列预测-LSTNet

STM32 DSP library MDK vc5\vc6 compilation error: 256, (const float64_t *) twiddlecoeff64_ 256, armBitRevIndexTableF64_ 256,
随机推荐
Qt新手入门级 计算器加、减、乘、除、应用
AD1256
String methods and instances
Vsual studio 2013 environment UDP multicast
简单却好用:使用Keras 2实现基于LSTM的多维时间序列预测
单播、组播、广播、工具开发、QT Udp通讯协议开发简介及开发工具源码
UDP通讯应用于多种环境的Demo
day5-jvm
Day2 websocket+ sort
Yolov5 learning summary (continuously updated)
JUC并发编程基础(1)--相关基础概念
Detailed explanation of KMP code distribution
Channel attention and spatial attention module
MySql与Qt连接、将数据输出到QT的窗口tableWidget详细过程。
使用Keras和LSTM实现对于长期趋势记忆的时间序列预测-LSTNet
Learning rate optimization strategy
MySQL基础---约束
[deep learning] teach you to write "handwritten digit recognition neural network" hand in hand, without using any framework, pure numpy
MySQL foundation - constraints
MySql下载,及安装环境设置