Using Java programming Problem 2.2 (Finding Duplications) (hint: read chapter 7
ID: 3757264 • Letter: U
Question
Using Java programmingProblem 2.2 (Finding Duplications) (hint: read chapter 7 slides about arrays for tallying) Write a program to sign people up for a seminar. Everybody is assigned an ID number in [O, 30]. It will start with a simple routine that asks users to type in their number. Once you have the array of up to 30 numbers (each one in [O, 30], you will want to check whether some people may have entered their numbers twice or not. Write a program, which can show the following information: ALL numbers in the array that have duplicates The two most frequently appeared numbers a b.
Explanation / Answer
MaxElement.java :-
import java.util.*;
public class MaxElement
{
public static void main(String []args)
{
int n,i,j,c,sno=1;
Scanner scan=new Scanner(System.in);
System.out.println("Enter array size between 0 to 30: ");
n=scan.nextInt();
int a[]=new int[n];
int f[]=new int[n];
System.out.println("Enter element value between 0 to 30:");
for(i=0;i<n;i++)
{
System.out.println(" a["+(i+1)+"]=");
a[i]=scan.nextInt();
f[i]=-1;
}
for(i=0;i<n;i++)
{
c=1;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
c++;
f[j]=0;
}
}
if(f[i]!=0)
{
f[i]=c;
}
}
int max=-1,maxi=0;
System.out.println("S.No Elements Frequency");
System.out.println(" ");
for(i=0;i<n;i++){
if(f[i]!=0)
{
System.out.println(sno+" "+a[i]+" "+f[i]+" ");
sno++;
if(f[i]>max){
max=f[i];
maxi=i;
}
}
}
System.out.println("Most Frequent Element is "+a[maxi]);
int second_max=-1,smaxi=0;
for(i=0;i<n;i++){
if(f[i]!=0)
{
if((f[i]>second_max)&&(f[i]!=max)){
second_max=f[i];
smaxi=i;
}
}
}
System.out.println("Second Most Frequent Element is "+a[smaxi]);
}
}
I'm providing executable code.
I've verified all the cases so you can simply run and enjoy.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.