# Array: Count occurrence of numbers

Question: Write a program that generates random integers between 1 and 100 and counts the occurrences of each.

```package Array;

import java.lang.Math;

/**
* Write a program that generates random integers between 1 and 100 and counts the occurrences of each.
* Created by aarushi on 8/5/21.
*/
public class Ch7Ex3 {

/*
Step 1: Generate random numbers between 1 and 100 and store them in an array
Step 2: Create a function that counts the occurrence of each number
Step 2.1: Create an array count which will store the count of the numbers
Step 2.2: Initialize count array with zero
Step 2.3: Loop through the length of count and increase the count on the index of the elements
in array (count[array[i]]++)
Step 3: Display count where count>0
*/

public static void main (String [] args){
int [] array= new int ;
createArray(array);
int [] count= countOccurrence(array);
display(array, count);
}

public static void createArray (int [] array){
for (int i=0; i<array.length; i++){
do{
array[i]= (int)(Math.random()*100);
} while (array[i]==0); //integer has to be between 1 and 100
System.out.print(array[i] + " ");
}
System.out.println();
}

public static int [] countOccurrence (int [] array){
int [] count= new int ;
for (int i=0; i<count.length; i++){
count[i]=0;
}

for (int i=0; i<array.length; i++){
count[array[i]]++;
}
return count;
}

public static void display (int [] array, int [] count){
for (int i=0; i<count.length; i++){
if (count[i]>0){
System.out.println(i + " appears " + count[i] + " times");
}
}
}
}
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.