Counting Number Frequency in Java

Counting number frequency in JavaOver the past couple weeks I have noticed a lot of students asking questions about counting number frequencies. The student is often generating, or asked to take in input, and count how many times a series of numbers appear in the data. Sometimes they are given a file for the input and other times they are asked to take in numbers entered by the user and count them. So I thought it would be helpful to show a quick example of how to go about taking in numbers and counting how many times they appear using an array in Java. Hopefully those new to Java can then use this information and tailor it to their individual needs.

Counting Numbers Using an Array

One of the first parts where new programmers get stuck is setting up the correct size of array for their number counts. The array should be the size of the possible values that can be created. In our demo below we are going to parse a real set of lottery numbers from LottoMax in Canada. The game is played by drawing 7 numbers ranging from 1 to 49 with an extra number called the “power ball” that we are going to ignore for this example. Since the numbers range from 1 to 49, 50 is the size of our array. Not hundreds (the number of draws) or 7, the number of numbers drawn a game. We want to count how many times the numbers themselves come up across hundreds or thousands of draws.

Since arrays start at zero, instead of having to deal with indexes that minus 1 all the time, we will simply keep zero in the mix and setup the array to hold 50 numbers, 0 – 49 and since number 0 is never drawn, it should have a count of zero when we are done. If you want to remove zero, just don’t print it and start at index 1. Let’s take a look at the code and then explain a bit of how it works.

public static void main(String[] args) {
    try {
        // Open our comma separated lotto number file
        BufferedReader reader = new BufferedReader(new FileReader("C:\\lottomax.csv"));
        // Create array to hold 50 digits, 0 through 49 (even though drawn numbers are between 1 and 49 inclusive)
        int[] numbers = new int[50];
        // Initialize all counts to zero.
        for (int i = 0; i < 50; i++ ){
            numbers[i] = 0;
        // Read each line of the file and split the columns using the commas
        String draw;
        while ((draw = reader.readLine()) != null) {
            String[] pieces = draw.split(",");
            // For columns 4 through 10 we are going to take the number, convert it to an integer
            // and use that integer as the index for our array. Increment the count by 1 for that index.
            for (int j = 4; j <= 10; j++) {
                int drawnNumber = Integer.parseInt(pieces[j]);
        // Loop through array of counts to print them and their frequency.
        for (int i = 0; i < 50; i++) {
            System.out.println(i + ": " + numbers[i]);
    } catch (Exception e) {
        System.out.println("There was a problem opening and processing the file.");

In the code above we open a a comma separated file (CSV) file on our computer. I have included this Lottomax numbers file here for you to use. We open the file and then go about creating our 50 integer array which will hold the counts for our 1-49 numbers. Meaning that if the number 1 is drawn, we are going to increment the value at numbers[1] and if we draw 23 we are going to increment the value at numbers[23].

We read each line and then split it into the various columns of data. Since the split is going to produce strings, we are going to need to read columns 4 through 10 (which are the actual numbers) and convert them to integers before we can use them as indexes. We loop through each of the 7 numbers, find the correct index in the array and increment its value by 1. We will do this for all numbers drawn across all games. When the loop is done, then our array is going to hold the total count for each number. So all that is left is to loop through them and print them out.

Different Project Variations On Counting Frequencies

Some of the projects people will run into include asking the user for the number or to have the number first generated within some range randomly. These variations will not alter the method too much. Here we used a file, but asking the user to enter a number just means we are going to take the number, validate it to make sure it is in the proper range and then use it as the index in our array. This might also include a loop to keep prompting the user for their next number. If we are generating the number, again we are going to check to make sure it is in the range we expect and use it as the index of the array.

This process can also be applied to more than just numbers. We can also use this to count letters, vowels or any kind of set of unique elements. The trick is just to make sure that the array which will hold the counts is large enough for all possible outcomes. You don’t want an array that will keep track of counts for 1 – 49 and then you generate a number like 99.

What If The Range Is Not In Serial Order or Is UnKnown?

In the cases where you are trying to count values that are either not known or not naturally in serial order (for instance like words from a file) an simple array is not going to work for you. But the principle can still be the same if you use other structures like Lists or other types of collection objects. Perhaps you are tasked with counting how many times the word “the” appears in a file. Instead of trying to use “the” as an index in a standard array (which won’t work) you can still use the word “the” as a key in a dictionary, hashmap or similar collection object. The values would be the same… an integer representing a count.

Try the code out and see how it works. The principle is pretty straight forward and can serve as a base for much more complex programs which build things like histogram charts etc. Thank you again for reading and happy programming! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 18 years. He works for a hot application development company in Vancouver Canada which service some of the biggest telecoms in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.