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 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.
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...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[]...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] strings = new String[100];        for (int i = 0; i < strings.length; i++) {            System.out.println("Enter Strings: stop ");            strings[i]=input.nextLine();            if(strings[i].equalsIgnoreCase("stop"))                break;        }               MaxLength(strings);//here is the error    }    public static int MaxLength(String[] array) {        int max = array[0].length();       ...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT