对于这个问题,我要编写一个名为mode的方法,它返回一个整数数组中最常出现的元素。假设数组至少有一个元素,并且数组中的每个元素都具有0到100之间的值(包括0和100)。通过选择较低的值来打破联系。
例如,如果传递的数组包含值{27,15,15,11,27},则您的方法应返回15.(提示:您可能希望查看本章前面的Tally程序以获取如何解决这个问题的想法。)
我遇到了查看特定输入出错的问题。例如:
模式({27,15,15,27,11,11,11,14,15,15,16,19,99,100,0,27})返回15,这是正确的,但模式({1 ,1,2,3,3})当它应为1时返回3.
以下是代码:
public static int mode(int[] input) {
int returnVal = input[0]; // stores element to be returned
int repeatCount = 0; // counts the record number of repeats
int prevRepCnt = 0; // temporary count for repeats
for (int i=0; i<input.length; i++) { // goes through each elem
for (int j=i; j<input.length; j++) { // compares to each elem after the first elem
if (i != j && input[i] == input[j]) { // if matching values
repeatCount++; // gets the repeat count
if (repeatCount>=prevRepCnt) { // a higher count of repeats than before
returnVal=input[i]; // return that element
}
prevRepCnt = repeatCount; // Keeps the highest repeat record
}
repeatCount=0; // resets repeat Count for next comparison
}
}
return returnVal;
}
答案 0 :(得分:4)
这是一种解决此问题的简单方法。创建一个名为count的大小为101的数组。索引(0-100)表示您计算的数字。遍历输入数组并计算每个数字的出现次数。最后,比较计数以找到最多的一个(领带变为较低的数字):
public static int mode(int[] input) {
int[] count = new int[101];
//count the occurrences
for (int i=0; i < input.length; i++) {
count[input[i]]++;
}
//go backwards and find the count with the most occurrences
int index = count.length-1;
for (int i=count.length-2; i >=0; i--) {
if (count[i] >= count[index])
index = i;
}
return index;
}
答案 1 :(得分:2)
我最近分析了四种计算阵列模式的方法:
您可以在本文中找到所有四种方法和性能比较的源代码(尽管在C#中):Finding Mode of an Array
答案 2 :(得分:1)
我会声明另一个变量来跟踪&#34;较低的值&#34;。并且当它具有相同的计数时,检查input [i]值是否小于lowerValue变量。注意我将&gt;分开了&安培; =根据你的情况。
int lowerValue;
public static int mode(int[] input) {
int returnVal = input[0]; // stores element to be returned
int repeatCount = 0; // counts the record number of repeats
int prevRepCnt = 0; // temporary count for repeats
int lowerValue = Integer.MAX_VALUE; // initalize it with the highest integer value - 2147483647
for (int i=0; i<input.length; i++) { // goes through each elem
for (int j=i; j<input.length; j++) { // compares to each elem after the first elem
if (i != j && input[i] == input[j]) { // if matching values
repeatCount++; // gets the repeat count
if (repeatCount>prevRepCnt) { // a higher count of repeats than before
returnVal=input[i]; // return that element
lowerValue = returnVal; // set the variable lowerValue to be the lower value
}
else if (repeatCount == prevRepCnt) && (input[i] < lowerValue) { // if it's the same number of count, take in whichever number is lower
returnVal=input[i]; // return that element
lowerValue = returnVal; // set the variable lowerValue to be the lower value
}
prevRepCnt = repeatCount; // Keeps the highest repeat record
}
repeatCount=0; // resets repeat Count for next comparison
}
}
return returnVal;
}
答案 3 :(得分:0)
根据您的代码,您需要更改的是。
public static int mode(int[] input) {
int returnVal = input[0]; // stores element to be returned
int repeatCount = 0; // counts the record number of repeats
int prevRepCnt = 0; // temporary count for repeats
for (int i=0; i<input.length; i++) { // goes through each elem
for (int j=i; j<input.length; j++) { // compares to each elem after the first elem
if (i != j && input[i] == input[j]) { // if matching values
repeatCount++; // gets the repeat count
if (repeatCount>=prevRepCnt) { // a higher count of repeats than before
returnVal=input[i]; // return that element
}
prevRepCnt = repeatCount; // Keeps the highest repeat record
}
repeatCount=0; // resets repeat Count for next comparison
}
}
return returnVal;
}
这是你需要改变的地方。
if (repeatCount>prevRepCnt) { // a higher count of repeats than before
答案 4 :(得分:0)
下面是我使用HashMaps的方法。
它将通过返回最小值来处理多模式数组。
using Nito.AsyncEx;
async Task CreateLongLivedTask(PauseToken pauseToken, CancellationToken cancellationToken)
{
while (true)
{
await pauseToken.WaitWhilePausedAsync(cancellationToken);
DoImportantStuff();
await Task.Delay(100, cancellationToken); // 10 loops per second
}
}