Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting Occurrences With An Array - Java

Tags:

java

arrays

I'm lost as how to compare 100 randomly generated number between 0-9 to an array value, also between 0-9, and then print the results. Be easy on me, I'm new to coding and I know I suck. I feel as though I'm 75% there. I know there are ways to make some of the code less redundant, however I seem to struggle with those techniques.

Here's what I have so far:

public static void main(String[] args) {
    double randomNum = 0;
    for (int i = 0; i < 100; i++) {
        randomNum = Math.random() * 10;

    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;
    int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (double j = 0; j <arrayNums.length; j++){
        if (arrayNums[0] == randomNum) {
            count0++;
        }
        else if (arrayNums[1] == randomNum){
            count1++;
        }
        else if (arrayNums[2] == randomNum){
            count2++;
        }else if (arrayNums[3] == randomNum){
            count3++;
        }else if (arrayNums[4] == randomNum){
            count4++;
        }else if (arrayNums[5] == randomNum){
            count5++;
        }else if (arrayNums[6] == randomNum){
            count6++;
        }else if (arrayNums[7] == randomNum){
            count7++;
        }else if (arrayNums[8] == randomNum){
            count8++;
        }
        else{
            count9++;
        }

    }
    System.out.print("Occurrences of 0: " + count0);
    System.out.print("\nOccurrences of 1: " + count1);
    System.out.print("\nOccurrences of 2: " + count2);
    System.out.print("\nOccurrences of 3: " + count3);
    System.out.print("\nOccurrences of 4: " + count4);
    System.out.print("\nOccurrences of 5: " + count5);
    System.out.print("\nOccurrences of 6: " + count6);
    System.out.print("\nOccurrences of 7: " + count7);
    System.out.print("\nOccurrences of 8: " + count8);
    System.out.print("\nOccurrences of 9: " + count9);
    }
}

}

Any and all help is appreciated.

like image 920
Monocyte Avatar asked Aug 03 '26 19:08

Monocyte


1 Answers

  • Using a HashMap as a counter is more elegant than keeping a separate variable for each item.
  • Use Random.nextInt(10) to generate a random number between 0 to 9 (inclusive).

        Map<Integer, Integer> counter = new HashMap<Integer, Integer>();
        Random rand = new Random();
        for (int i = 0; i < 100; i++) {
            int randomNum = rand.nextInt(10);
            Integer currentCount = counter.get(randomNum);
            if (null == currentCount) {
                counter.put(randomNum, 1);
            } else {
                counter.put(randomNum, currentCount+1);
            }
        }
        for (Integer key : counter.keySet()) {
            System.out.println(key + " -> " + counter.get(key));
        }
    

Example output

0 -> 12
1 -> 10
2 -> 9
3 -> 6
4 -> 8
5 -> 9
6 -> 11
7 -> 12
8 -> 14
9 -> 9
like image 118
Nir Alfasi Avatar answered Aug 06 '26 08:08

Nir Alfasi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!