In: Computer Science
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I want to be able to scan and output which number appears the most and the least.
int x =1000
int[] array = new array[x]
for(int i = 0 ; i < x; i++){
array[i] = random.nextInt(101);
}
import java.util.Random; public class MostLeastOccurring { public static void main(String[] args) { Random random = new Random(); int x = 1000; int[] array = new int[x]; for (int i = 0; i < x; i++) { array[i] = random.nextInt(101); } int leastOccurring = array[0], mostOccurring = array[0], leastCount = 0, mostCount = 0; for (int i = 0; i < x; i++) { int count = 0; for (int j = 0; j < x; j++) { if (array[i] == array[j]) ++count; } if (i == 0 || count > mostCount) { mostCount = count; mostOccurring = array[i]; } if (i == 0 || count < leastCount) { leastCount = count; leastOccurring = array[i]; } } System.out.println("Most occurring number is " + mostOccurring + ", it occurs " + mostCount + " times."); System.out.println("Least occurring number is " + leastOccurring + ", it occurs " + leastCount + " times."); } }