Skip to content

Instantly share code, notes, and snippets.

@JiaruiTracy
Created July 12, 2017 14:59
Show Gist options
  • Save JiaruiTracy/ff184386b19486e18aec5f9aad8502bc to your computer and use it in GitHub Desktop.
Save JiaruiTracy/ff184386b19486e18aec5f9aad8502bc to your computer and use it in GitHub Desktop.
A quick way to calculate repeat times of each element in an array, which contains 100 random numbers between 0 and 30
package arr;
import java.util.Random;
public class arrTest {
public static void main(String[] args) {
int[] randNum = new int[100];
int[] count = new int[31];
Random r = new Random();
for(int i = 0;i<100;i++){
randNum[i] = r.nextInt(30)+1;
System.out.print(randNum[i]+".");
count[randNum[i]]++;
}
System.out.println();
for(int i=1;i<31;i++){
System.out.print(count[i]+",");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment