Character Frequency Array - Page 2

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Sep 2005
Posts
74

## The irony of it all...

I turned the assignment in today, and the professor rejected the way I did it. Basically she told me that it was the most inefficent way possible, and to use a frequency array.

So can someone explain exactly what a frequency array IS (she didn't do a very good job =p), so I don't spend 2 hours on something that should take 30mins?

Thanks.

2. ## Well....

Its an array of frequencies I presume, but I dind't know that it was a defined
algorithm concept. The solution I presented at the bottom of page 1 of this thread
uses such an array and it only scans the charsequence once. You can't do less
scanning than that if you want to get the job done.

The bad thing about that solution is that it is far from an object oriented
approach, it's just "grunt" sequential programming that has resulted in a
concrete block of code.

The other solution I presented (the long winded one,... remember ? ) is in
my opinion a far better solution.
Last edited by sjalle; 10-31-2005 at 03:11 PM.

3. Registered User
Join Date
Sep 2005
Posts
74
I would use that approach, however the teacher insists that I use a "frequency" array. She wants it to look something like this:

Input: "Hello World"

a: 0, b: 0, c: 0, d: 1, e: 1, f: 0, g: 0, h: 1, i: 0, j: 0, k: 0, l: 3, m: 0, n: 0, o: 2, p: 0, q: 0, r: 1, s: 0, t: 0, u: 0, v: 0, w: 1, x: 0, y: 0, z: 0

Most frequent char: l (3 time(s))

Somehow she insists I can make an array of 26 counters, and store the "frequencies" of the letters there..and then output the most frequent one. Although I'm not sure how to store the letters in the array, and keep track of their count(s).

4. ## I repeat:

The last solution I posted in this thread does exactly that, and it will
work for other alphabets also, not just the 26 characters that seems to
be the only characters in your teachers universe.

My solution uses an int array of 256 ints, all initially 0. I see no point in
fiddling with a subset of 26 or whatever, it just clutters up the code.

As I scan the characters I use each character (integer cast) to select the
correct int in the array and increment this by 1. When done I just scan
the 256 ints for the highest value. The index of the highest value is the
character with the highest frequency.

mkay ?

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise