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) {           }
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); }   ...
public static GuitarType forDisplayName(String displayName) { if(displayName.equals("Electric")) return ELECTRIC; else if(displayName.equals("Acoustic")) return ACOUSTIC; else if(displayName.equals("Classical")) return...
public static GuitarType forDisplayName(String displayName) { if(displayName.equals("Electric")) return ELECTRIC; else if(displayName.equals("Acoustic")) return ACOUSTIC; else if(displayName.equals("Classical")) return CLASSICAL; return null; } Write this if condition using for loop. public static GuitarWood forDisplayName(String displayName) { if(displayName.equals("Indian Rosewood")) return INDIAN_ROSEWOOD; else if(displayName.equals("Sitka")) return SITKA; else if(displayName.equals("Brazilian Rosewood")) return BRAZILIAN_ROSEWOOD; else if(displayName.equals("Maple")) return MAPLE; else if(displayName.equals("Alder")) return ALDER; else if(displayName.equals("Mahogany")) return MAHOGANY; else if(displayName.equals("Adirondack")) return ADIRONDACK; else if(displayName.equals("Cocobolo")) return COCOBOLO; else if(displayName.equals("Cedar")) return CEDAR; return null; } Write the if condition into for loop
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT