0
Implement the below function to print the frequency of these characters b, f, j, p, v in a given string and also print the coun
Example Input: Buffet Output: b=1, f=2, others=3 Input2 : aabcdhar Output: a=3, b=1, c=1,d=1,h=1,r=1 Tried String s = "hihelloBye"; char [] c = s.toCharArray(); // char search = 'l'; int uniq = 0; boolean []b = new boolean[c.length]; for (int i = 0; i< c.length; i++) { if(!b[i]) { int count = 0; if ( c[i] == 'h') { count++; b [i] = true; } else if (c[i] == 'e') { count++; b[i] = true; } else if (c[i] == 'b') { count++; b[i] = true; } if (count == 1) { uniq +=1; } else if(count > 1) { System.out.println(c[i] + " = " + count); } } } System.out.println("Uniq: " + uniq);
3 Answers
+ 1
Syt -> Show Your Try
0
wait a min
0
String s = "hihelloBye";
char [] c = s.toCharArray();
// char search = 'l';
int uniq = 0;
boolean []b = new boolean[c.length];
for (int i = 0; i< c.length; i++)
{
if(!b[i])
{
int count = 0;
if ( c[i] == 'h')
{
count++;
b [i] = true;
}
else if (c[i] == 'e')
{
count++;
b[i] = true;
}
else if (c[i] == 'b')
{
count++;
b[i] = true;
}
if (count == 1)
{
uniq +=1;
}
else if(count > 1)
{
System.out.println(c[i] + " = " + count);
}
}
}
System.out.println("Uniq: " + uniq);