Why the loop or a recursion running infinite???
import java.util.Arrays; import java.util.Scanner; public class Main{ static int k,x,p,s,y; static Scanner cout=new Scanner(System.in); static int arrsize=cout.nextInt(); static int[]arr=new int[arrsize]; public static void f2(int s ){ System.out.println(arr[s]); return; } public static void f1(int arrsize,int p){ ++y; ++k; int u=0; s=u; x=arr[p]; for(;y<arrsize;){ if(x==arr[y]){ ++u; } } if(s<u){ s=u; } if(k==arrsize){ f2(s); } if(k==arrsize){ return; } else if(y==arrsize-1){ f1(arrsize,++p); } } public static void main(String args[]){ for(int i=0;i<arrsize;i++){ arr[i]=cout.nextInt(); } f1(arrsize,p); } }