Question

In: Computer Science

public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';   ...

public static char mostFrequent(String str) {
       if(str.length()==0) {
           return '0';
       }
       String temp="";
       for (int i = 0; i < str.length(); i++) {
                if(!temp.contains(String.valueOf(str.charAt(i)))) {
                    temp += String.valueOf(str.charAt(i));
                }
            }
       char[] tempArray=stringToArray(temp);
       int[] countArr=new int[tempArray.length];
       int max=0;
       for(int i=0;i<tempArray.length;i++) {
           int cnt=numOccurences(tempArray[i],str);
           countArr[i]=cnt;
           if(max<cnt) {
               max=cnt;
           }
       }
       for(int i=0;i<tempArray.length;i++) {
           if(max==countArr[i]) {
               return tempArray[i];
           }
       }
       return '0';
      }

CAN SOMEONE PLEASE COMMENT OUT THIS CODE, I DON'T KNOW EXACTLY WHAT IT DOES, THANKS.

Solutions

Expert Solution

below is well commented code
This code basically returns the character in string having maximum frequency.

public static char mostFrequent(String str) {
//check if string length is zero, then return 0 as answer
if(str.length()==0) {
return '0';
}
String temp="";
// this loop basically adds all the unique characters of str to temp
for (int i = 0; i < str.length(); i++) {
// checks if a char is in temp or not, if not add it to temp
if(!temp.contains(String.valueOf(str.charAt(i)))) {
temp += String.valueOf(str.charAt(i));
}
}
// convert string into char array
char[] tempArray=stringToArray(temp);
// new count array of integer
int[] countArr=new int[tempArray.length];
int max=0;
for(int i=0;i<tempArray.length;i++) {
// count occurence of each character in str
int cnt=numOccurences(tempArray[i],str);
countArr[i]=cnt;
//update maximum count
if(max<cnt) {
max=cnt;
}
}
for(int i=0;i<tempArray.length;i++) {
//return element with maximum count
if(max==countArr[i]) {
return tempArray[i];
}
}
return '0';
}
Thank you, please upvote


Related Solutions

public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string...
public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string and remove the characters so it becomes a mirrored word. example: Input: qswwbi Output: 4 remove q s b i "wow" is a mirror word. output : 0 Input : "if" output : 1 remove either I or f because it's two words. don't use 3rd parties library or java.util.
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
#include <iostream> using namespace std; string* callMe(string*& s){ static string* str = new string{"Chiqita Banana"}; str...
#include <iostream> using namespace std; string* callMe(string*& s){ static string* str = new string{"Chiqita Banana"}; str = s; return str; } int main() { string* s = new string{"Hey Google"}; string* str = callMe(s);    cout << "Call me \'" + *str +"\'"; return 0; } 1. What is the output or the error of this program? Call me 'Hey Google' 2. And please explain the data flow in detail (to memory level)? Please help question 2 with explanation, thank...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;        int number = 0;        Scanner in = new Scanner(System.in);        System.out.print("Enter a letter: ");        letter = in.next().charAt(0);        if(letter == 'A' || letter == 'B' || letter == 'C') number = 2;        if(letter == 'D' || letter == 'E' || letter == 'F') number = 3;        if(letter == 'G' || letter ==...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str)...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text. For example, s.indexOf("Mississippi", "sip") returns 6. Hint: You must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.
Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string...
Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string */ if (c == 0) return s; /* convert the first character to upper case*/ if (c >= ‘a’ && d <= ‘z’) { c -= 32; s[0] = c; } /* convert the remaining characters*/ strtoupper(s + 1); return s; }
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up,...
public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up, ‘d’ for down, ‘l’ for left or ‘r’ for up. And in case the user enters anything except one of these four letters, it will be kept asked to enter a new char until the entered char matches one of them. (in java) *note (do just complete this specific method as this is just one of the methods apart of this.)
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT