Question

In: Computer Science

JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared...

JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared the X amount of times. (For example there are 3 numbers which occured 50 times so I want the output to show which 3 numbers occured 50 times) This is the code

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

Solutions

Expert Solution

Algorithm: We can use a frequency array to store the count of all elements which are in range of (0,101).

Have a look at the below code. I have put comments wherever required for better understanding.

import java.util.Random;


class Main {

    public static void main(String[] args) {
        Random random = new Random();
        int x = 1000;

        int X = 10; // Take X as 10 as sample case, then this prgram will print all numbers which occurs 10 times, it can be anything
        int[] array = new int[x];
        for (int i = 0; i < x; i++) {
            array[i] = random.nextInt(101);
        }
        // create a frequence array
        int[] count = new int[101];
        // populate the frequency array
        for (int i=0;i<x;i++){
          count[array[i]]+=1; 
        }
        // Display all those elements whose count is X 
        for (int i=0;i<101;i++){
          if (count[i]==X){
            System.out.println(i);
          }
        }




        
    }
}

Happy Learning!


Related Solutions

How do you determine the slope of a line? Is there more than one way to...
How do you determine the slope of a line? Is there more than one way to determine the slope? Why or Why not? How do you find the intercepts of a line? Explain using an example.
a) when more than one charge is present how do you determine the electric field at...
a) when more than one charge is present how do you determine the electric field at an empty point in space nearby? b) Describe the experimental evidence that supports your statement. c) Give one example described above in everyday life relating to your statement
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!! QUESTION 1 Consider the following program segment: int i = 2; int n = 18; while (i < n) {     if ((i % 2) == 0) i++; else n--; } System.out.println( i ); What is the value of n when the above program...
“Are Women Really More Talkative Than Men?” is the title of a 2007 article that appeared...
“Are Women Really More Talkative Than Men?” is the title of a 2007 article that appeared in the journal Science. In the article, Mehl and colleagues report the results of a study of 396 men and women. Each par-ticipant wore a microphone that recorded every word he or she uttered. The researchers counted the number of words uttered by men and women and compared them. The data below are fictional but they re-create the pattern that Mehl and colleagues observed:...
WRITE IN JAVA PLEASE~ A string may use more than one type of delimiter to bracket...
WRITE IN JAVA PLEASE~ A string may use more than one type of delimiter to bracket information into "blocks". The primary ways to divide things up into block is by the braces { } , parentheses ( ), and brackets [ ] as delimiters. A string is considered properly delimited if each right delimiter ] } or )( is matched with a preceding left delimiter { [ ( of the same type in such a way that either the resulting...
JAVA question A string may use more than one type of delimiter to bracket information into...
JAVA question A string may use more than one type of delimiter to bracket information into "blocks". The primary ways to divide things up into block is by the braces { } , parentheses ( ), and brackets [ ] as delimiters. A string is considered properly delimited if each right delimiter ] } or )( is matched with a preceding left delimiter { [ ( of the same type in such a way that either the resulting blocks of...
1- Create a Java program to determine a certain number whether that number is odd or...
1- Create a Java program to determine a certain number whether that number is odd or even. Hint : use arithmetic operators and expressions to find the odd or even numbers. 2- Create a Java program to ask a user to enter a name and password as shown below: name is “Ahmed” and his password is 2321 or name is “Ali” and his password is 6776 . The program shows a greeting “Hi ..Welcome to my program” if the user...
A psychologist wants to determine whether speaking more than one language fluently has cognitive benefits. They...
A psychologist wants to determine whether speaking more than one language fluently has cognitive benefits. They conduct a study in which a total of 72 participants are tested on a task measuring cognitive control. Half the participants are male, and half are female and speak one, two, or three+ languages fluently. For a two-way ANOVA with SSbetween = 148; SSLanguage = 80; SSSex = 50; SStotal = 350, what can the psychologist conclude? Be sure to address all hypotheses. Assume...
Is carrying more than one EpiPen advisable?
Is carrying more than one EpiPen advisable?
how a single gene can result in more than one RNA product? and What is the...
how a single gene can result in more than one RNA product? and What is the GU-AG rule and how does it contribute to this process?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT