写一个查找数组中出现次数最多的数
时间: 2024-05-12 12:16:58 浏览: 99
可以使用HashMap来解决此问题。首先遍历一遍数组,将数组中的每个数作为key,出现次数作为value存入HashMap中。然后遍历一遍HashMap,找到value最大的key即可。
Java代码如下:
```java
public static int findMostFrequent(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
// 统计每个数出现的次数
for (int num : arr) {
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
int maxCount = 0, maxNum = arr[0];
// 找出出现次数最多的数
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
maxNum = entry.getKey();
}
}
return maxNum;
}
```
相关问题
用c语言查找数组中出现次数最多的数
可以使用哈希表来查找出现次数最多的数。
具体步骤如下:
1. 声明一个哈希表,用来记录每个数出现的次数。
2. 遍历数组,将每个数在哈希表中对应的值加1。
3. 找到哈希表中值最大的键,即为出现次数最多的数。
以下是示例代码:
```c
#include <stdio.h>
#define SIZE 1000
int main()
{
int arr[SIZE];
int n, max_num = -1, max_count = 0;
int hash[SIZE] = {0};
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
hash[arr[i]]++;
if (hash[arr[i]] > max_count) {
max_count = hash[arr[i]];
max_num = arr[i];
}
}
printf("The number that appears the most is %d, which appears %d times.\n", max_num, max_count);
return 0;
}
```
注:此方法假设数组中所有数均为正整数且小于等于SIZE。如果不是,则需要调整SIZE的大小。
用c语言查找数组中出现次数最多的数不使用循环的代码
在 C 语言中,要找出数组中出现次数最多的元素而不用循环,可以利用哈希表(如标准库中的`<string.h>`中的`strchr()`函数)的思想,但这通常需要额外的空间开销,并且假设数组元素是整数并且范围较小,能够放入`int`类型的哈希表。这是一种非典型的方法,因为传统上查找最常出现的元素会涉及遍历数组。这里提供一个简单的示例,注意这个方法效率不高,适用于特定场景:
```c
#include <stdio.h>
#include <string.h>
// 假设数组元素为整数,且范围不大于 'size'
#define ARRAY_SIZE 100
int find_most_frequent(int arr[], int size) {
// 创建一个大小等于数组元素范围的最大值的哈希表
int hash[ARRAY_SIZE] = {0};
for (int i = 0; i < size; ++i)
hash[arr[i]]++;
// 找到最大计数值,也就是出现次数最多的元素
int max_count = 0, most_frequent = arr[0];
for (int i = 0; i < ARRAY_SIZE; ++i) {
if (hash[i] > max_count)
max_count = hash[i], most_frequent = i;
}
return most_frequent;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 9};
int size = sizeof(arr) / sizeof(arr[0]);
int most_common = find_most_frequent(arr, size);
printf("出现次数最多的数是 %d\n", most_common);
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)