-3

Basically it's a program to take 6 input numbers from user, store them in an Array, calculate their Mode.

package p18;
import java.util.Scanner;
public class P18 {
 public static void main(String[] args) { 
    Scanner S=new Scanner(System.in);
    int[] arr1=new int [6];
    for (int i = 0; i < 6; ++i) {
    int g = S.nextInt();
    arr1[i] = g;
 }
 int input=6;
 for(int i=0;i<input;i++)
   int count=0;
 for(int j=0;j<input;j++)
 {
   int temp=arr1[j];    
   int tempco=0;
   for(int p=0;p<input;p++)   
    if(arr1[p].equals[temp])
   tempco++;
   if(tempco>count)
   {
     int t=temp;
     count=tempco; 
   }}
 System.out.println("the most frequent number"+t+, +count); 
}}
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46

1 Answers1

0

I think that this is the best possible way to do it. Use a HashMap, with indexes the elements of the array and the value of each position the number of occurences of that number in the array.

Scanner S=new Scanner(System.in);
    int[] arr1=new int [6];
    for (int i = 0; i < 6; ++i) {
       int g = S.nextInt();
       arr1[i] = g;
    }
    S.close();
    int t = 0;
    int count = 0;
    int input=arr1.length;

    for(int j=0;j<input;j++){
        int temp=arr1[j];    
        int tempco=0;
        for(int p=0;p<input;p++){   
            if(arr1[p]==temp)
                tempco++;
            if(tempco>count){
                t=temp;
                count=tempco; 
            }
        } 
    }
    System.out.println("the most frequent number is "+String.valueOf(t)+"with "+String.valueOf(count)+" occurrences");

There you go, the solution with your code corrected. You have several mistakes in your code so please don't get used to ask for code solutions as a routine, that way you're not going to learn anything and this web is not for that, it's to ask for concrete questions or mistakes that you don't know not to do a full program

a.ras2002
  • 385
  • 2
  • 7
  • 21
  • Sorry but no, try yourself and when you have a full programm and a punctual doubt or problem try to post it on stackoverflow and see if anyone can help you – a.ras2002 Jun 29 '16 at 06:54
  • Mr a.ras i have try myself plz see my new code of MCQS. In this question there is a separate array of question, key of question, options, Answers. The user seen the Questions and their four options and will enter the answer. The answer array will be compared with the key array which option is the true display correct answer – Rana Rajpoot Jun 29 '16 at 07:54