# Thread: is there a more efficient way?

1. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

## is there a more efficient way?

Hi, I've been asked to build s method that finds the most frequent used letter (char).
I thought about a nested for loop that has two pointers and compares between the first and the second in the common way and when finding a match, increment a counter by one. Eventually returns the letter that counted the most.

My question is about the complexities because I'm not sure this is the most efficient way, and Ideas?

Thanks.

2. I would use a HashMap<Character, Integer> and increment each char's count when a char is encountered. Either that or an array of int.

3. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

## unfortunately I'm not allowed

to use that, I can use the String methods for my task.

I thought about creating an array but then if I go through it and then sort it, the complexity will be the same..

I thought also about going through the list ones and store each char in its designated place, for ex. if I come across 'a', an int a variable will be incremented and so on, so the outcome will look like:
a = 3
b = 1
c = 2

Then all I have to do is return the highest valued letter, in that case 'a'
but how do I do that without going through the list again (for complexity reasons)?

By the way it's recursive..

4. You could use an array and use the 0th item for 'a', the next item for 'b'. The array index can easily be obtained by using char math, since the index to the array would be nothing more than your char - 'a' (assuming all chars are lower-case, and if not, first converting them to lower case). Then you could increment the array item associated with that index. When done, you could easily find the largest item in the array, and via its index, know which char it represents.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•