Search code examples
javaarraysperformancecounting

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


I've input the value by

    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]);
    }

And I've sorted it already by

   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;
            }
        }
    }

Problem is here

    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);
    }

The problem is I wasn't to use dupCount to count the most duplicated value if it's >=2 is "more". but the program runs incorrectly.

Example program I prefer

input = 5 // output = 5

input = 1112223333 // output = 3

input = 01223605504 // output = 0

input = 10003444 // output = more


Solution

  • You can easily do it using Map and Collections. Given below is the program with sample tests:

    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());
            }
        }
    }
    

    Output:

    5
    3
    0
    More
    

    Note:

    1. You do not need to sort the number array with this solution.
    2. The logic works on the property of Map by virtue of which it replaces the existing entry if a new entry with the existing key is put into the it.

    Rest of the logic is straight forward. Additionally, I have put important comments inside the code to make it more comprehensible. Feel free to comment in case of any doubt.