+ 1

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); } }

3rd Sep 2021, 3:07 PM
Shruti Shukla
Shruti Shukla - avatar
3 Respostas
0
Input is =6 1,4,4,4,5,3
3rd Sep 2021, 3:10 PM
Shruti Shukla
Shruti Shukla - avatar
0
Improve your chances for answers by editing your post to include a code bit link rather than a raw text code like this. Less hassle for those who wanted to help you out, line number reference supported, and they can even write suggestion or fix in the comments section (only viewable in apps). How to share code link ... https://www.sololearn.com/post/75089/?ref=app
3rd Sep 2021, 3:40 PM
Ipang
0
y does not increase in f1() loop: for ( ; y<arrsize; ) { if (x == arr[y]) { ++u; } }
3rd Sep 2021, 3:50 PM
zemiak