当前位置:网站首页>Enter n integers and output the number of occurrences greater than or equal to half the length of the array
Enter n integers and output the number of occurrences greater than or equal to half the length of the array
2022-06-26 18:11:00 【A goblin】
Cattle from · Written examination of Internet famous enterprises / Interview question bank
One , Title and description :
Input n It's an integer , The number of output occurrences greater than or equal to half the length of the array
Input description :
Each test input contains n Space separated n It's an integer ,n No more than 100, among There's an integer More than or equal to n/2.
Output description :
The number of output occurrences is greater than or equal to n/2 Number of numbers .
Two , Example
Input
3 9 3 2 5 6 7 3 2 3 3 3
Output
3
3、 ... and , Solution ideas and code
!!!! First, notice that there is only one such number
Ideas 1:
Sort the input sequence , The element in the middle position means that the occurrence times are greater than or equal to n/2 Number of numbers
Code :
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.nextLine(); // Input received as string , And each character is separated by a space
String[] s = str.split(" "); // Split the string with spaces
int[] arr = new int[s.length]; // Prepare an array
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(s[i]); // Convert the element to type and move it into the prepared array
}
System.out.println(arr[arr.length/2]);// Just take out the middle element
}
}
}Ideas 2:
utilize Map Count the number of occurrences of each element , Output Value Greater than or equal to n/2 Corresponding key that will do
Code :
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.nextLine(); // Input received as string , And each character is separated by a space
String[] s = str.split(" "); // Split the string with spaces
Integer[] arr = new Integer[s.length]; // Prepare an array (Integer best ,int It's fine too , Use at this time Map, So avoid unpacking with the packing type )
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(s[i]); // Convert the element to type and move it into the prepared array
}
Map<Integer,Integer> map = new HashMap<>();
for (int j = 0; j < arr.length; j++) {
// Count the number of each element
Integer count = map.get(arr[j]); // Take out the current key, Corresponding to Value
if(count == null){
map.put(arr[j],1); // If it's the first time to put , namely Value Set as 1
} else {
map.put(arr[j],count+1);// Otherwise, on the original basis +1
}
}
for(Map.Entry<Integer,Integer> entry : map.entrySet()){
// Traverse Map Find an array that is greater than or equal to half the length of the array Value Corresponding key
if(entry.getValue() >= (arr.length/2)){
int x = entry.getKey();
System.out.println(x);
break;
}
}
}
}
}边栏推荐
- Chen Qiang: Alibaba's 100 billion level large-scale digital business knowledge map helps business growth
- 临时关闭MySQL缓存
- 零时科技 | 智能合约安全系列文章之反编译篇
- 行锁与隔离级别案例分析
- LeetCode 238 除自身以外数组的乘积
- Analysis of deep security definition and encryption technology
- Binary search-2
- Detailed explanation of dos and attack methods
- 我想知道,我在肇庆,到哪里开户比较好?网上开户是否安全么?
- 请指教同花顺开户选选择哪家券商比较好?现在在线开户安全么?
猜你喜欢

小程序设置按钮分享功能

Concept and working principle of data encryption standard (DES)

ISO documents

No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete

VCD-影音光碟

RSA加密解密详解

Data Encryption Standard DES security

The king of Internet of things protocol: mqtt

数据加密标准(DES)概念及工作原理

wm_concat()和group_concat()函数
随机推荐
[QNX] Command
CD-CompactDisk
决策树与随机森林
Get and set settings in 26class
Decision tree and random forest
LeetCode 128最长连续序列
wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet
How about opening an account at Guojin securities? Is it safe to open an account?
How to add an application to the deviceidle whitelist?
Eigen库计算两个向量夹角
(必须掌握的多线程知识点)认识线程,创建线程,使用Thread的常见方法及属性,以及线程的状态和状态转移的意义
Analysis of deep security definition and encryption technology
Paging query and join Association query optimization
图像二值化处理
pycharm的plt.show()如何保持不关闭
Row lock analysis and deadlock
手写promise.all
判断某个序列是否为栈的弹出序列
Plt How to keep show() not closed
sql中的几种删除操作