I'm given a code that asks us to have the user input a set of integers(they can be positive or negative). Then the code goes through the list and counts the occurence of each number. Once it reaches the end, the code displays ONLY the number with the most occurrences, or numbers if they both have the same # of occurences an it is the largest # of occurences. (Ex. The user inputs [4, 3, 7, 4, 5, 3], and the program displays 'The input numbers with the most occurences are [4, 3], and they occur 2 times.). I have written most of the code, but now I am stuck on how to get it to display only the numbers with the highest occurence! Please help point me in the right direction!

import java.util.*;
public class CountOccurence {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a list of integers(values can be either positive or negative). Enter 0 to terminate input.");
    int numInput = input.nextInt();
    Integer[] listOfNumbers = formNumberList(numInput); 
    Map<Integer, Integer> numberList = new HashMap<Integer, Integer>();
    while(true) {
        if (numInput != 0) {
            for (int i = 0; i < listOfNumbers.length; i++) {
                Integer numberKey = listOfNumbers[i];

                if(numberKey > 0) {
                    if (!numberList.containsKey(numberKey)) {
                        numberList.put(numberKey, 1);
                    }
                    else {
                        int numberCount = numberList.get(numberKey);
                        numberCount++;
                        numberList.put(numberKey, numberCount);
                    }

                }
            }
        }
        else
            if (numInput == 0)
                break;
    }

    }

    public static Integer[] formNumberList(int numInput) {
        Integer[] number = new Integer[numInput];
        return number;
    }
}

Dani AI

Generated

Two things to fix before you try to pick the winning number(s): the input handling and the selection logic. In your posted code the first integer is treated as an array size (via formNumberList(numInput)), the while(true) never updates numInput (so it’s effectively broken), and the if (numberKey > 0) test ignores negative values. is right about scanning the counts map for the highest count — below is a concise, working example that reads until 0, counts all integers (including negatives), finds the maximum frequency, and prints every number that reaches that frequency.

import java.util.*;

public class CountOccurrence {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter integers (0 to end):");
        List<Integer> numbers = new ArrayList<>();
        while (true) {
            if (!sc.hasNextInt()) { sc.next(); continue; }
            int n = sc.nextInt();
            if (n == 0) break;
            numbers.add(n);
        }
        sc.close();

        if (numbers.isEmpty()) {
            System.out.println("No numbers entered.");
            return;
        }

        Map<Integer,Integer> counts = new LinkedHashMap<>();
        for (int n : numbers) counts.put(n, counts.getOrDefault(n, 0) + 1);

        int max = Collections.max(counts.values());
        List<Integer> winners = new ArrayList<>();
        for (Map.Entry<Integer,Integer> e : counts.entrySet())
            if (e.getValue() == max) winners.add(e.getKey());

        Collections.sort(winners); // optional: consistent output order
        System.out.printf("The input numbers with the most occurrences are %s, and they occur %d time%s.%n",
                          winners, max, (max == 1 ? "" : "s"));
    }
}

Notes/troubleshooting:

  • If you want to preserve the order of first appearance, keep the LinkedHashMap and skip Collections.sort(winners).
  • If you only want duplicates (ignore max==1), check if (max > 1) before printing.
  • Add debug prints of counts if results look wrong — that quickly shows whether counting or input is the issue.

You can loop through the numberList examining each number and its count. Take the first number as being the highest and store it in another variable e.g. int highestCount.
For each number in the list, compare it's count to highestCount. If it is higher, replace highestCount with the new count and store the number as well.
If a number has the same count as highestCount you'll want to keep it as well, dumping it only when highestCount is again replaced but another higher value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.