counting number of occurances

I have an array of queues,

eg:

1, 2 ,3

4, 5 , 6

1, 8, 9

2, 4, 5

so everytime i poll each queue I get the value of the first column. I need to count the occurances of each value. Eg for this example, 1 appear twice, 4 appear once and 2 appear once. How can I do this efficiently since I may have to fetch up to 1000 queue records?

Thanks.

[387 byte] By [javaPOTATOa] at [2007-11-26 19:22:58]
# 1

you could try (untested):

private int occurances[9];

constructor {

for (int i = 0; i < occurances.length; i++) {

occurances[i] = 0;

}

}

public void analyseQueue(ArrayList queue[]

for (int i = 0; i < queue.length; i++) {

ArrayList currentQ = queue[i];

for (int n = 0; n < currentQ.size(); n++) {

int n = (int) currentQ.get(n);

occurances[n - 1]++;

}

}

public int getOccurances(int n) {

return occurances[n - 1];

}

abu5ea at 2007-7-9 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi,could u add some comment into the code cause i don't really understand how it flows. thanks.
javaPOTATOa at 2007-7-9 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 3

Don't ask for much do ya!

private int occurances[9];

//the above array holds a value for each number up to the

//maximum number we are looking for (presumed to be 9).

//the array will hold an int representing how many times each

//value has been found in a queue.

constructor { //rename this so it is a proper constructor.

for (int i = 0; i < occurances.length; i++) {

occurances[i] = 0; //initialise all values of the array to zero.

}

}

public void analyseQueue(ArrayList queue[])

for (int i = 0; i < queue.length; i++) { //for each queue:

ArrayList currentQ = queue[i]; //extract the queue

for (int n = 0; n < currentQ.size(); n++) { //for each element in the queue:

int n = (int) currentQ.get(n); //extract the integer value of the element.

occurances[n - 1]++; //as this number has been found, increase the

//number of occurances of it. Note the -1 as an array is zero indexed.

}

}

public int getOccurances(int n) {

return occurances[n - 1]; //again, because zero indexed.

}

abu5ea at 2007-7-9 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 4
***PLEASE NOTE: This code has NOT been tested. Whatever you do, don't just submit it for coursework/pass it on to your CEO/sell it to make millions, or whatever reason you have for this code. You MUST test it first.
abu5ea at 2007-7-9 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 5
Thank you very much! =0)
javaPOTATOa at 2007-7-9 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 6

Yes. Rest assured I will have to test it as it's part of a system I'm trying to develop. =p

> ***PLEASE NOTE: This code has NOT been tested.

> Whatever you do, don't just submit it for

> coursework/pass it on to your CEO/sell it to make

> millions, or whatever reason you have for this code.

> You MUST test it first.

javaPOTATOa at 2007-7-9 21:44:09 > top of Java-index,Java Essentials,Java Programming...