Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Counting in arrays

Zak Tacc
Greenhorn
Posts: 25
Let's say I have a sorted single dimensional String array like the following:

0: apple
1: grape
2: grape
3: grape
4: orange
5: orange
6: pear
7: pear
8: pear
9: pear
10: watermelon

How would I appropriately store the data in an object array array that has two data members: String fruit and int frequency, with fruit being the type of fruit and frequency being the number of times each fruit appears in the array? (in this array, each fruit will only be listed once)

Thanks

Nicola Garofalo
Ranch Hand
Posts: 308
You could use a Map

as a key you can use the fruit name, as a value its cardinality.

Zak Tacc
Greenhorn
Posts: 25
Nicola Garofalo wrote:You could use a Map

as a key you can use the fruit name, as a value its cardinality.

I don't really know what you mean by that, but the second array needs to figure out each individual fruit name by itself

Nicola Garofalo
Ranch Hand
Posts: 308
Probably i didn't get you too.

You need an algorithm to solve the problem or the data structure you could use?

For example you could use a two dimensional Object array

in the first dimension you store the Strings representing the distinct names of the fruits.
in the second dimension you store Integer values representing the frequencies of the fruits

Zak Tacc
Greenhorn
Posts: 25
I'm looking more for the algorithm. I'm supposed to use a single dimensional array, with each element of the array being a different variation of the same object.

Zak Tacc
Greenhorn
Posts: 25
What my program is actually doing is taking input from any data file, breaking it up into separate words and storing them in an array. And then I'm supposed to produce output like the following:

Word Number Frequency Word
1 25 the

(word number, frequency, and word are separate columns. tabbing doesn't seem to work in this forum too well)

So far I have the input file broken up and put into an array called "library", and I've created a Word class with data members String word and int frequency (with the appropriate accessors) . I'm just not too sure how to count the frequency of each word, or identify each word in library[] as unique.

thanks again

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
if you have to store this in an array, about the only thing you can do is this:

read a word
search the array to see if the word is already there
if it is, increment the counter
if not, add it with a counter of '1'

I'm not sure what 'word number' refers to...is it the position of the word in the original string? how do you store that after the 1st one? in other words (ha ha), if your string is "fred is fred", what would your final array look like?

1 fred 2
2 is 1

or

1 fred 1
2 is 1
3 fred 2

or something else?

Zak Tacc
Greenhorn
Posts: 25
the word number is just a counter of each unique word. it just increments by 1 every time there's another unique word, there's no particular order. The two values that matter are the name of the unique word and the number of times it appears in the library[].

pete reisinger
Ranch Hand
Posts: 46
Hi,
do you need something like this?

where data is array with words (duplicates as well).

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
Pete,

We try to not hand out answers here. People learn better when they write the code themselves!

thanks!

pete reisinger
Ranch Hand
Posts: 46
I'm sorry, I didn't realize that. Won't happen again.