How do I solve the Q without using any kind of map or something as I havent learned it yet?
I cannot understnad how I will store values of each char and print final ans
@code_breaker-1001 Bro you can also do this question without map by using array. Just create an array of size as ASCII length and every index will be be corresponding to the ASCII number of the character. Then iterate through the string and update the frequency at every iteration. Then at last return the character with max frequency with the help of the array. I have attached the code for this problem through array below. Have a look at it for reference.
import java.util.*;
public class Main {
static final int ASCII_SIZE = 256;
static char getMaxOccuringChar(String str)
{
int count[] = new int[ASCII_SIZE];
int len = str.length();
for (int i=0; i<len; i++)
count[str.charAt(i)]++;
int max = -1;
char result = ' ';
for (int i = 0; i < len; i++) {
if (max < count[str.charAt(i)]) {
max = count[str.charAt(i)];
result = str.charAt(i);
}
}
return result;
}
public static void main(String[] args)
{ Scanner scn=new Scanner(System.in);
String str =scn.nextLine();
System.out.println(getMaxOccuringChar(str));
}
}
1 Like
Thank you so much. I never tht I could do it this way. Worked Perfectly…