查找数组中的整数模式

对于这个问题,我要编写一个名为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=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; } 

这是解决此问题的更简单方法。 创建一个名为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; } 

我最近分析了四种计算arrays模式的方法:

  • 如果数组中的数字范围很小,那么使用计数排序 – O(N)时间,(N)空间但非常有效。 此解决方案直接适用于您要问的问题,因为您在arrays中只有101个可能的值。
  • 哈希表中数组中的索引元素 – O(N)时间,O(N)空间。 如果从大范围取值,例如所有整数,则此解决方案适用。
  • 对数组进行排序,然后计算连续的相等元素 – O(NlogN)时间,O(1)空间。 如果数组太大而无法构建索引,则此解决方案适用。
  • 部分排序数组但跳过小于当前候选的分区–O(NlogN)时间,O(1)空间但比完全排序数组更有效,因为将跳过许多分区。

您可以在本文中找到所有四种方法和性能比较的源代码(尽管在C#中): 查找数组的模式

我会声明另一个变量来跟踪“较低的值”。 并且当它具有相同的计数时,检查input [i]值是否小于lowerValue变量。 注意我为您的条件分隔了>&=。

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; iprevRepCnt) { // 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; } 

根据您的代码,您需要更改的是。

 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=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 
  • 拿出等号,你应该是好的。