Ive been struggling with this for hours now and I just cant seem to get anything
ID: 3632982 • Letter: I
Question
Ive been struggling with this for hours now and I just cant seem to get anything :(Write a program that reads numbers from the keyboard into an array of type int[] you may assume that there will be 50 or fewer entries in the array. Your program allows any number of numbers to be entered, up to 50 numbers. The output is to be two column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest.
For the Array
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
output should be
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
Explanation / Answer
public static void main(String[] args) { // keyboard input Scanner kb = new Scanner(System.in); int[] input = new int[50]; int idx = 0; do { // prompt for input System.out.print("Enter a number (or 'q' to quit): "); // check if int was entered if(kb.hasNextInt()) input[idx++] = kb.nextInt(); // if not, quit input else break; } while(idx < 50); System.out.println(); // copy just the input into new array int[] data = new int[idx]; for(int i = 0; i = 0; i--) { // is distinct? for(int j = idx-1; j > i; j--) if(data[j] == data[i]) continue outer; // count occurrences int count = 0; for(int j = 0; jRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.