public static int[] Freq(int[] num){
int[] ans=new int[2];
Arrays.sort(num);
int maxFPar=1;int maxF=1;int maxNPar=num[0];int maxN=num[0];
for (int i=1;i< num.length;i++){
if (num[i]!=maxNPar){
maxNPar=num[i];maxFPar=1;
}
else {
maxFPar++;
}
if (maxFPar>maxF){
maxF=maxFPar;maxN=maxNPar;
}
}
ans[0]=maxF; ans[1]=maxN;******
//ans[0] denotes the max frequency and ans[1] denotes the number with the maximum frequency in the given num[] array.
return ans;
}`
Hi PRANEETH,
Can you please clarify if you are suggesting a better algorithm or are you asking for one?
i was suggesting a better algorithm…instead of using two for loops to iterate through each element…!!
1 Like
and yes, if there are any suggestions for a better algorithm ,always happy to learn/understand!
1 Like