Write a program that reads a list of integers from input and identifies the mode (the value that appears most often). The input is a sequence of integers that ends with -1. All other integers in the list are between 1 and 20 (inclusive). Total number of integers in the list is unknown. Output the mode and end with a newline. Assume that the list is not empty and only one mode exists.
Hint: Use an array to count the number of occurrences of 1-20. See comment in starter code.
Ex: If the input is: 5 9 2 2 1 4 5 5 -1, the output is: 5

This is my code:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);
int count=1;
int userInput;
int[] frequencies = new int[count];
int mode;
int maxCount;

while((userInput=scnr.nextInt())!=1){
if(userInput>=1&&userInput<=20){
count++;
for(int i=0; i maxCount){
maxCount=currCount;
mode=currNum;
}
}
System.out.println(mode);
}
}

Respuesta :

ACCESS MORE