Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description of a project(Counting the occurrences of numbers entered): • Write a

ID: 3627245 • Letter: D

Question

Description of a project(Counting the occurrences of numbers entered):


• Write a program that reads an unspecified number of integers and finds the one that has the most occurrences.
• Your input ends when the input is 0. For example, if you entered 2 3 40 3 5 4 -3 3 3 2 0, the number 3 occurred most often. Please enter one number at time.
• If not one but several numbers have the most occurrences, all of them should be reported. For example, since 9 and 3 appear twice in the list 9 30 3 9 3 2 4, both occurrences should be reported.

Explanation / Answer

Code:

import java.util.ArrayList;

import java.util.Scanner;

public class NumOccur

{

      public static void main(String[] args)

    {

            Scanner readData=new Scanner(System.in);

            int x,count=1;

            ArrayList<Integer> alist=new ArrayList<Integer>();

            ArrayList<Integer> datalist=new ArrayList<Integer>();

            ArrayList<Integer> occurlist=new ArrayList<Integer>();

            while(true)

            {

                  System.out.print("Enter the Number : ");

                  x=Integer.parseInt(readData.nextLine());

                  if(x==0)break;

                  alist.add(x);                

            }

            for(int i=0;i<alist.size();i++)

            {

                  count=1;

                  if(!datalist.contains(alist.get(i)))

                              datalist.add(alist.get(i));        

                  else

                        continue;

                  for(int j=i+1;j<alist.size();j++)

                  {

                        if(alist.get(i)==alist.get(j))

                        {

                              count++;

                        }

                  }

                  occurlist.add(count);

            }

            System.out.println(" Data              Number of Occurance");

            for(int i=0;i<datalist.size();i++)

            {

                  System.out.println(datalist.get(i)+" "+occurlist.get(i));

            }

    }

}

Output:

Enter the Number : 2

Enter the Number : 3

Enter the Number : 40

Enter the Number : 3

Enter the Number : 5

Enter the Number : 4

Enter the Number : -3

Enter the Number : 3

Enter the Number : 3

Enter the Number : 2

Enter the Number : 0

Data              Number of Occurance

2                 2

3                 4

40                1

5                 1

4                 1

-3                1