Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Incase you need more reference, here is my own code which works exactly as you want but don't use this algorithm and implement it directly as it will limit your thinking of generating new algorithms your way.(Migrated from the same question you asked on this linkthis link at StackOverflow.

Incase you need more reference, here is my own code which works exactly as you want but don't use this algorithm and implement it directly as it will limit your thinking of generating new algorithms your way.(Migrated from the same question you asked on this link at StackOverflow.

Incase you need more reference, here is my own code which works exactly as you want but don't use this algorithm and implement it directly as it will limit your thinking of generating new algorithms your way.(Migrated from the same question you asked on this link at StackOverflow.

a bit clearer + typo fixes
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

This algorithm will work perfectly fine for what you want to develop:

Before that, let me make it clear that you mentioned aAbout the situation where you want to check repeatitions ofhave 2 different characters the same number of times, for that you have to run a for loop in reverse to identify the 2nd character. So if the 2nd character is not same as the first one identified, and also if it'sits number of repeatitionsrepetitions are the same, you print both the characters or else, just print the single character you find at the first for loop because both the characters are going to be same.

This algorithm will work perfectly fine for what you want to develop:

Before that, let me make it clear that you mentioned a situation where you want to check repeatitions of 2 different characters same number of times, for that you have to run a for loop in reverse to identify the 2nd character. So if the 2nd character is not same as the first one identified, and also if it's number of repeatitions are the same, you print both the characters or else, just print the single character you find at the first for loop because both the characters are going to be same.

About the situation where you have 2 different characters the same number of times, for that you have to run a for loop in reverse to identify the 2nd character. So if the 2nd character is not same as the first one identified, and also if its number of repetitions are the same, you print both the characters or else, just print the single character you find at the first for loop because both the characters are going to be same.

added 1116 characters in body
Source Link

Before that, let me make it clear that ifyou mentioned a situation where you want to check repeatitions of 2 different characters same number of times, for that you have to run a for loop in reverse to identify the 2nd character. So if the 2nd character is not same as the first one identified, and also if it's number of repeatitions are the same, you print both the characters or else, just print the single character you find at the first for loop because both the characters are going to be same.

Run a reverse for loop like this given below in your length function after the first for loop. Make sure to reset the variables that you are going to use again. This will identify another position of the variable that is repeating maximum times. But since a function can only return 1 value, use this reverse algorithm in a different function and have what you want to achieve.

for(int i=size-1;i>0;i--){  
   if(A[i].equals(A[i-1])){  
       current++;  
   }  
   else{  
       if(current>res){  
           index=i-current;  
           res=current;  
       }  
       current=1;  
   }  
}  

Incase you need more reference, here is my own code which works exactly as you want but don't use this algorithm and implement it directly as it will limit your thinking of generating new algorithms your way.(Migrated from the same question you asked on this link at StackOverflow.

Before that, let me make it clear that if you want to check repeatitions of 2 different characters same number of times, you have to run a for loop in reverse to identify the 2nd character. So if the 2nd character is not same as the first one identified, and also if it's number of repeatitions are the same, you print both the characters or else, just print the single character you find at the first for loop because both the characters are going to be same.

Before that, let me make it clear that you mentioned a situation where you want to check repeatitions of 2 different characters same number of times, for that you have to run a for loop in reverse to identify the 2nd character. So if the 2nd character is not same as the first one identified, and also if it's number of repeatitions are the same, you print both the characters or else, just print the single character you find at the first for loop because both the characters are going to be same.

Run a reverse for loop like this given below in your length function after the first for loop. Make sure to reset the variables that you are going to use again. This will identify another position of the variable that is repeating maximum times. But since a function can only return 1 value, use this reverse algorithm in a different function and have what you want to achieve.

for(int i=size-1;i>0;i--){  
   if(A[i].equals(A[i-1])){  
       current++;  
   }  
   else{  
       if(current>res){  
           index=i-current;  
           res=current;  
       }  
       current=1;  
   }  
}  

Incase you need more reference, here is my own code which works exactly as you want but don't use this algorithm and implement it directly as it will limit your thinking of generating new algorithms your way.(Migrated from the same question you asked on this link at StackOverflow.

added 87 characters in body
Source Link
Loading
added 405 characters in body
Source Link
Loading
Source Link
Loading