int[] a = new int[10]{1,2,3,4,5,6,7,7,7,7};
Run Code Online (Sandbox Code Playgroud)
我该怎么写一个方法并返回7?
我希望在没有列表,地图或其他助手的帮助下保持原生.只有数组[].
我需要做作业才能获得阵列中最"流行"的数字(最高频率的数字),如果有多个数字具有相同数量的节目,请随机获取一些数字.经过三个多小时的尝试,并在网上搜索,这就是我得到的:
public int getPopularNumber(){
int count = 1, tempCount;
int popular = array[0];
int temp = 0;
for ( int i = 0; i < (array.length - 1); i++ ){
if ( _buses[i] != null )
temp = array[i];
tempCount = 0;
for ( int j = 1; j < _buses.length; j++ ){
if ( array[j] != null && temp == array[j] )
tempCount++;
}
if ( tempCount > count ){
popular = temp;
count = tempCount;
}
}
return …Run Code Online (Sandbox Code Playgroud) 在Java中是否存在像"Set"这样的对象,它只能包含唯一的字符串值,还包含字符串值出现次数的计数?
这个想法很简单
有了数据集ala ...
A B B C C C.
我想将每行文本添加到类似Set的对象中.每次将非唯一文本添加到集合中时,我还希望具有与集合关联的数值,以显示添加的次数.因此,如果我在上面的数据集上运行它,输出将是这样的:
答:1 B:2 C:3
有任何想法吗?