Java 使用简单的for循环查找模式(数组中最频繁的值)?
如何使用简单的for循环查找数组中最频繁的模式值 代码编译时输出错误 以下是我所拥有的:Java 使用简单的for循环查找模式(数组中最频繁的值)?,java,arrays,methods,mode,Java,Arrays,Methods,Mode,如何使用简单的for循环查找数组中最频繁的模式值 代码编译时输出错误 以下是我所拥有的: public static void mode(double [] arr) { double mode=arr[0]; for(int i = 1; i<arr.length; i++) { if(mode==arr[i]) { mode++; } } return mod
public static void mode(double [] arr)
{
double mode=arr[0];
for(int i = 1; i<arr.length; i++)
{
if(mode==arr[i])
{
mode++;
}
}
return mode;
}
-只需使用一个HashMap,其中包含数组索引值作为键,它们的出现号作为值 -在遍历for循环时,通过检查HashMap中是否已经存在当前索引来更新HashMap。如果它真的这样做了,那么在HashMap中找到double并查看它已经出现了多少次,然后再出现一次,将它放回HashMap中 -我是用Java做的,因为这看起来就像你正在使用的一样。同样好的是,时间复杂度是这类场景可能得到的最佳时间复杂度,因为您必须至少访问每个元素一次 -如果你有这样一个双精度数组:{1,2,3,1,1,1,5,5,7,7,7,7,7,7} 然后散列映射在末尾看起来像这样:{1->4,2->1,3->1,5->3,7->9} 意味着1发生了4次,2发生了1次。。。。7例发生9次等
public static double mode(double [] arr)
{
HashMap arrayVals = new HashMap();
int maxOccurences = 1;
double mode = arr[0];
for(int i = 0; i<arr.length; i++)
{
double currentIndexVal = arr[i];
if(arrayVals.containsKey(currentIndexVal)){
int currentOccurencesNum = (Integer) arrayVals.get(currentIndexVal);
currentOccurencesNum++;
arrayVals.put(currentIndexVal, currentOccurencesNum );
if(currentOccurencesNum >= maxOccurences)
{
mode = currentIndexVal;
maxOccurences = currentOccurencesNum;
}
}
else{
arrayVals.put(arr[i], 1);
}
}
return mode;
}
首先我按顺序对数组排序,然后计算一个数字的出现次数。只有循环和if语句没有哈希映射 我的代码:
static int Mode(int[] n){
int t = 0;
for(int i=0; i<n.length; i++){
for(int j=1; j<n.length-i; j++){
if(n[j-1] > n[j]){
t = n[j-1];
n[j-1] = n[j];
n[j] = t;
}
}
}
int mode = n[0];
int temp = 1;
int temp2 = 1;
for(int i=1;i<n.length;i++){
if(n[i-1] == n[i]){
temp++;
}
else {
temp = 1;
}
if(temp >= temp2){
mode = n[i];
temp2 = temp;
}
}
return mode;
}
这段代码是另一种不使用hashmaps的方式。这个用java创建的方法以数组作为参数,并在方法中创建另一个名为numberCount的数组。此数组numberCount将其索引设置为数组中的值。包含传递的数组中的值的numberCount索引将在numberCount++numberCount[array[i]]的值上加1,然后将转到数组中的下一个值重复,直到数组结束。然后创建另一个for循环,以遍历numberCount中数组的每个值,该索引具有最高值/计数,将被存储并返回为max。此方法必须经历一些困难的更改才能使用双数组。但是对于int数组似乎效果很好
public static int findMostFrequentValue(int[] array) {
int i;
int[] numberCount = new int[100];
for (i = 0; i < array.length; i++)++numberCount[array[i]];
int max = 0;
int j;
for (j = 0; j < numberCount.length; j++) {
if (numberCount[j] > max) max = j;
}
return max;
}
您应该检查数组中每个元素的发生次数。您可以通过2个内部for循环将数组的每个元素与自身和其他元素进行比较 请记住,如果数组未排序,并且包含超过1个模态值,因此重复出现次数,则将返回第一个模态值。最好先按Arrays.sortarray对数组进行排序,以便选择最小或最大的模态值
public static int modeOfArray(int[] array){
int mode;
int maxOccurance = 0;
for(int i=0; i<array.length; i++){
int occuranceOfThisValue = 0;
for(int j=0; j<array.length; j++){
if(array[i] == array[j])
occuranceOfThisValue++;
}
if(occuranceOfThisValue > maxOccurance){
maxOccurance = occuranceOfThisValue;
mode = array[i];
}
}
return mode;
}
谢谢你的回复。我可以使用for loop.public static double modedouble[]arr来解决这个问题{double loc=0,val=-1;forint i=1;iYes jlss4e yours肯定能工作,但由于嵌套的for循环,它必须将每个元素与其他元素进行比较。因此它必须运行n*n-1次。因此,如果有100个输入,它就必须运行100*99次。如果您不担心性能,它看起来很完美虽然=-是的,我是编程新手,我还没有学习过Hashmaps。是的,我肯定会学习你最喜欢的语言的Hashmaps。我希望我刚开始时能更多地使用它们。这可能是将键值信息存储在一个列表中而不必创建对象的最简单方法。比如姓名和电话号码列表等等如果需要双数组,请将int[]更改为double[]