如何计算 Java 中可能具有相同计数的最重复值

How to count the most duplicated value that may have equal count in Java

我已经通过

输入了值
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String  lines = br.readLine();
    String[] strs = lines.trim().split("");
    int x = 0;
    int[] numbers = new int[strs.length];
    for (int i = 0; i < strs.length; i++) {
        numbers[i] = Integer.parseInt(strs[i]);
    }

我已经按

排序了
   int temp = 0;
    for (int i=0; i < numbers.length; ++i) {
        for (int j=1; j < (numbers.length - i); ++j) {
            if (numbers[j-1] > numbers[j]) {
                temp = numbers[j-1];
                numbers[j-1] = numbers[j];
                numbers[j] = temp;
            }
        }
    }

问题在这里

    int numDup = 0, dupCount = 1, maxCount = 0;
    int previous = -1;
    for (int i=0; i < numbers.length; ++i) {
        if (numbers[i] == previous) {
            ++numDup;
            if(maxCount < numDup){
                maxCount = numDup;
                dupCount = 1;
            }
            else {
                dupCount += 1;
            }
        }
        else {
            previous = numbers[i];
            numDup = 1;
        }
    }
    if(dupCount >= 2){
        System.out.println("more");
    }
    else{
        System.out.println(dupCount);
    }

问题是我没有使用 dupCount 来计算重复次数最多的值,如果它 >=2 是 "more"。 但程序运行不正确。

我喜欢的示例程序

input = 5 // output = 5

input = 1112223333 // output = 3

input = 01223605504 // output = 0

input = 10003444 // output = more

有两个问题

  1. 在 for 循环中,您使用的是 ++i 而不是 i++
  2. 你需要 else if 块而不是 else where maxCount == numDup 那么 dupCount 应该增加。
int numDup = 0, dupCount = 1, maxCount = 0;
       int previous = -1;
       for (int i=0; i < numbers.length; i++) {
           if (numbers[i] == previous) {
               ++numDup;
               if(maxCount < numDup){
                   maxCount = numDup;
                   dupCount = 1;
               }else if(maxCount == numDup) {
                   dupCount ++;
               }
           }else {
               previous = numbers[i];
               numDup = 1;
           }
       }
       if(dupCount >= 2){
           System.out.println("more");
       }
       else{
           System.out.println(dupCount);
       }

您可以使用 MapCollections 轻松完成。下面给出的是带有示例测试的程序:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String args[]) {
        printDupCount("5");
        printDupCount("1123321233");
        printDupCount("01223605504");
        printDupCount("10440034");
    }

    static void printDupCount(String lines) {
        String[] strs = lines.trim().split("");
        int[] numbers = new int[strs.length];
        for (int i = 0; i < strs.length; i++) {
            numbers[i] = Integer.parseInt(strs[i]);
        }

        // Put the count of each number into a map
        Map<Integer, Integer> groupMap = new HashMap<Integer, Integer>();
        for (int n : numbers) {
            if (groupMap.containsKey(n))
                groupMap.put(n, groupMap.get(n) + 1);
            else
                groupMap.put(n, 1);
        }

        // Find the maximum count
        Map.Entry<Integer, Integer> maxEntry = null;
        for (Map.Entry<Integer, Integer> entry : groupMap.entrySet()) {
            if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
                maxEntry = entry;
            }
        }

        // Find the frequency of maximum count
        int freq = Collections.frequency(groupMap.values(), maxEntry.getValue());

        // Check if the frequency of maximum count matches with that of any other count
        boolean more = false;
        for (Map.Entry<Integer, Integer> entry : groupMap.entrySet()) {
            if (freq == Collections.frequency(groupMap.values(), entry.getValue())
                    && entry.getValue() != maxEntry.getValue()) {
                more = true;
                break;
            }
        }

        // Print the result
        if (more) {
            System.out.println("More");
        } else {
            System.out.println(maxEntry.getKey());
        }
    }
}

输出:

5
3
0
More

注:

  1. 您不需要使用此解决方案对数字数组进行排序。
  2. 逻辑适用于 Map 的 属性,如果将具有现有密钥的新条目放入其中,它会替换现有条目。

剩下的逻辑很简单。此外,我在代码中添加了重要注释以使其更易于理解。如有任何疑问,请随时发表评论。