How to create a method that takes input string and returns the most frequently occurring letter(s) in string?

Prototype of the function: public String Duplicate(String str)

Constraints:

  1. If there are two or more letters which are most frequently occuring, then return all possibilities in the output.
  2. The input string should always contain UPPERCCASE characters if any other characters (LOWERCASE) found Return -1.

Example 1:
Input String str = "ABDEFFFGHIJ"
Output The function Duplicate() returns "F"

Example 2:
Input String str= "HIJKLLLMMNOPQQR"
Output The function Duplicate() returns "LMNQ"

So what exactly is your question? What you specified is method declaration public String Duplicate(String str). For the letters you can separate the string into chars and count how many times each chart is in the string.
You can get the size of the string and the use a loop to get the chars out. Then simply compare the chars and if you find a match increase a counter number

what have you tried, where did you go wrong? Show some effort, we're not your personal homework service.

Are you asking for an algorithm to do this work? If yes, I will give you one. If you are looking for codes, then show me what you have got so far.

One algorithm which is easy but not very efficient is to use a hash-like structure. Each character is the key and the value of each key is the counting number. Iterate through each character of a given string. You would simply increase the total count of the character if found the key; otherwise, return -1. You may keep track of the highest count so far while iterating. If you successfully go through the loop, then look for the key(s) that has/have the highest count to be your return value. That's all.

(because chars are integer numeric values you can simply use them to index into an array of counts - in which case that algorithm is as efficient as you will get.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.