Computer Science, asked by khushi200696, 1 day ago

write a program to find out the characters with the highest frequency in string class​

Answers

Answered by prateekshaandpratu
0

Answer:

Algorithm:-

Copying the String character by character to LinkedHashMap.

If its a new character then insert new character , 1.If character is already present in the LinkedHashMap then update the value by incrementing by 1.

Iterating over the entry one by one and storing it in a Entry object.

If value of key stored in entry object is greater than or equal to current entry then do nothingElse, store new entry in the Entry object

After looping through, simply print the key and value from Entry object.

public class Characterop {

public void maxOccur(String ip) { LinkedHashMap<Character, Integer> hash = new LinkedHashMap(); for(int i = 0; i<ip.length();i++) { char ch = ip.charAt(i); if(hash.containsKey(ch)) { hash.put(ch, (hash.get(ch)+1)); } else { hash.put(ch, 1); } } //Set set = hash.entrySet(); Entry<Character, Integer> maxEntry = null; for(Entry<Character,Integer> entry : hash.entrySet()) { if(maxEntry == null) { maxEntry = entry; } else if(maxEntry.getValue() < entry.getValue()) { maxEntry = entry; } } System.out.println(maxEntry.getKey()); } public static void main(String[] args) { Characterop op = new Characterop(); op.maxOccur("AABBBCCCCDDDDDDDDDD"); }

}

Answered by cheemtu
0

hi hi hi

hi hi mark brainliest

Attachments:
Similar questions