Java 程序在 Java 中计算众数
在统计数学中,众数是出现次数最多的值。例如,假设值集为 3、5、2、7、3。此值集的众数为 3,因为它出现的次数超过任何其他数字。
算法
1.Take an integer set A of n values. 2.Count the occurrence of each integer value in A. 3.Display the value with the highest occurrence.
示例
public class Mode {
static int mode(int a[],int n) {
int maxValue = 0, maxCount = 0, i, j;
for (i = 0; i < n; ++i) {
int count = 0;
for (j = 0; j < n; ++j) {
if (a[j] == a[i])
++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}
return maxValue;
}
public static void main(String args[]){
int n = 5;
int a[] = {0,6,7,2,7};
System.out.println("Mode ::"+mode(a,n));
}
}输出
Mode ::7
广告
数据结构
联网
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP