java - 如何缩短我的两个方法?

标签 java arrays string methods

所以我编写了两种方法,一种是查找数组中最常见的数字,另一种是查找数组中最常见的名称。两者都极其复杂。

我了解如何解决每个问题,但我不确定如何浓缩我的方法。另外,我不允许使用数组方法

First, the most common digit method.

 public static int mostFrequentDigit(int[] a){
    int count0=0, count1=0, count2=0, count3=0, count4=0, count5=0, count6=0, count7=0, count8=0, count9=0;
    for (int i=0;i<a.length;i++){
     int digit=a[i]%10;
     if (digit=0) count0++;
      else if (digit=1) count1++;
      else if (digit=2) count2++;
      else if (digit=3) count3++;
      else if (digit=4) count4++;
      else if (digit=5) count5++;
      else if (digit=6) count6++;
      else if (digit=7) count7++;
      else if (digit=8) count8++;
      else count9++;
    }
    if (count0> count1, count2, count3, count4, count5, count6, count7, count8, count9);
     return count0;
    if (count1>count0, count2, count3, count4, count5, count6, count7, count8, count9);
     return count1;
    if (count2>count0, count1, count3, count4, count5, count6, count7, count8, count9);
     return count2;
    if (count3>count0, count1, count2, count4, count5, count6, count7, count8, count9);
     return count3;
    if (count4>count0, count1, count2, count3, count5, count6, count7, count8, count9);
     return count4;
    if (count5>count0, count1, count2, count3, count4, count6, count7, count8, count9);
     return count5
    if (count6>count0, count1, count2, count3, count4, count5, count7, count8, count9);
     return count6;
    if (count7>count0, count1, count2, count3, count4, count5, count6, count8, count9);
     return count7;
    if (count8>count0, count1, count2, count3, count4, count5, count6, count7, count9);
     return count8;
    else 
     return count9;
}}

Now, the most common name method.

public static String mostCommonName (String[] names) {
     int indexOfFrequency = 0;
     int indexOfMostFrequency = 0;
     String mostCommon = "";
     int frequency = 1;
     int mostFrequency = 1;
     for (int index = 0; index < names.length; index ++) {
       indexOfFrequency = index;
      for (int scan = index + 1; scan < names.length; scan ++) {
        if ((names[scan].compareTo(names[index])) == (0)) {
          indexOfFrequency = scan;
          frequency ++;
        }
      }
      if (frequency > mostFrequency) {
        mostFrequency = frequency;
        mostCommon = names[indexOfFrequency];
        indexOfMostFrequency = indexOfFrequency;
      }
      else if (frequency == mostFrequency) {
        if ((names[indexOfFrequency].compareTo(names[indexOfMostFrequency]))   < 0) {
          mostCommon = names[indexOfFrequency];
          indexOfMostFrequency = indexOfFrequency;
        }
        else {
          mostCommon = names[indexOfMostFrequency];
        }
      }
      frequency = 1;
    }
    return mostCommon;
    }

最佳答案

如果您只想考虑最低有效数字,则可以这样做。

int[] a = {1, 2, 3, 4, 5, 6, 1};
int mostCommon = IntStream.of(a)
         // get the lowest digit
        .mapToObj(i -> i % 10)
        // create a mapping of digits => count of digits
        .collect(Collectors.groupingBy(i -> i, Collectors.counting()))
        // take the entries
        .entrySet().stream()
        // sort them by count, descending.
        .sorted(Comparator.comparing(e -> -e.getValue()))
        // give me the first result.
        .findFirst()
        // if there was no result, throw an error.
        .orElseThrow(AssertionError::new)
        // give me the digit.
        .getKey();

System.out.println(mostCommon);

打印

1

如果你想要一个 Integer 的所有数字,你需要编写一个方法来返回所有数字的 Stream 并使用 .flatMap()

对于名称,代码是相同的,只是您需要 Stream.of(a).collect...

关于java - 如何缩短我的两个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33747012/

相关文章:

java - 如何找到包含两个唯一重复字符的最长子字符串

python - 如何从 Excel 单元格中读取列表

Javascript - 比较两个数组。如果两者都找到对象,则更改它

C - 带有结构数组的 printf 字符串

c - fgets 内部是如何工作的?

java - 验证字符串格式(模式)输入

java - Netty 服务器如何向不同的客户端发送数据?

java - 如何使用java检查mysql中的特定数据库是否已经存在

java - 如何在雪豹上安装 java jdk 7

javascript - 根据包含的数组内容过滤对象数组。 Vanilla JS,lodash,还有其他一些 React 相关技术吗?