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...
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.
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 ==...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it returns 0 if (words == null OR words.length == 0) // returns number of times letter is the first letter of a String in words array // use equalIgnoreCase to check if letter and str.charAt(0) are equal and ignoring case }
import java.util.Scanner; public class Lab5 { public static void main(String[] args) { final char SIDE_SYMB =...
import java.util.Scanner; public class Lab5 { public static void main(String[] args) { final char SIDE_SYMB = '-'; final char MID_SYMB = '*'; Scanner scanner = new Scanner(System.in); String inputStr = ""; char choice = ' '; int numSymbols = -1, sideWidth = -1, midWidth = -1; do { displayMenu(); inputStr = scanner.nextLine(); if (inputStr.length() > 0) { choice = inputStr.charAt(0); } switch (choice) { case 'r': System.out.println("Width of the sides?"); sideWidth = scanner.nextInt(); System.out.println("Width of the middle?"); midWidth = scanner.nextInt();...
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 BagInterface<String> intersection (BagInterface<String> bagA, BagInterface bagB) This method must return a new bag which...
public static BagInterface<String> intersection (BagInterface<String> bagA, BagInterface bagB) This method must return a new bag which is the intersection of the two bags: bagA and bagB. An element appears in the intersection of two bags the minimum of the number of times it appears in either. For example, {1,1,2} ∩{1,1,2,2,3}= {1,1,2}. Do not forget to state the big Oof your method implementation. two file java main and test by netbeans java data structure and algorithem
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT