Fashion in an array

0

I try to see what is wrong with the code but I still have not been able to find an answer, please help

public int moda(int[] a){
  int moda=0;
  int Rep=0;

  for(int i=0;i<a.length-1;i++) {
    int cantRep=0;
   for(int j=0;j<a.length-1;i++) {

     if(a[i]==a[j])

     cantRep++;

     if(cantRep>Rep) moda=a[i];
     Rep=cantRep;

   }
  }

  return moda;
}
    
asked by Juan Andrés Loco 23.09.2018 в 03:14
source

1 answer

0

I can not understand your code well but the little I can understand is that in your condition

 if(a[i]==a[j])

You're just iterating once, so you only have one for

 for(int i = 0; i < limite; i++{
   if(a[i]==a[j])
  }

What you can understand is that you are comparing a data from your array "to []" in index i with another data in your same array "a []" in index j, here the error is that never enter the index j, you have to add another for, like this:

 for(int i = 0; i < limite; i++{
  for(int j = 0; j < limite; j++{
   if(a[i]==a[j]){
      //condición
     }
    }
  }

If you could better explain your question and your code could give you a better solution to your problem

    
answered by 23.09.2018 / 07:44
source