Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Max Repeated Number in array
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int maxCount=0,maxNum=-1; int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } HashSet<Integer> hash=new HashSet<>(); int temp=0; for(int i:arr){ if(!hash.contains(i)){ hash.add(i); temp=getCount(i,arr); if(temp>maxCount){ maxCount=temp; maxNum=i; }else if(temp==maxCount){ if(maxNum<i){ maxNum=i; } } } } System.out.println("Number:"+maxNum+" Repeat:"+maxCount); } public static int getCount(int num,int[] arr){ int res=0; for(int i:arr){ if(i==num){ res++; } } return res; } }
run
|
edit
|
history
|
help
0
hw1p1
Remove punctuation except numbers, commas and spaces
Escaping Literals (Table of records)
Test.java
Cola Machine
classwork
amit
Finding Biggest Square
Series_4_8
Construct Tree from Ancestor Matrix