Question

In: Computer Science

JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I...

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);

}

Solutions

Expert Solution

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.");
    }
}


Related Solutions

Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
     i What values would be stored in the given variables in each case? a.          int n =...
     i What values would be stored in the given variables in each case? a.          int n = 12 % 5; b.          double x = 15 % 11 + 5.3 - 5 / (2.5 - 0.3); c.   float y = 2 / (3.5 + static_cast<int>(3.5)); d.   bool z = (6 – 7 <= 2 * 1) && (5 + 4 >= 3) || (6 + 2 != 17 – 3 * 10);
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13    count: 5    pid: 13342 $> query: 22   ...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT