Trying to count occurences in an array list then display the statistics
Posted on 2004-10-12
I have an array list of length 10,000 which hold the heights of binarysearch trees. My job is to take this array list go through it and count how many occurences of heights.
Example this is a sample small array list...
2 this array list after i went through it would read
6 HEIGHTS oCCURENCES
2 2 3
2 5 1
3 6 1
i have thought this through and because it is an arraylist of 10,000 i'm trying to optimize ther algorith. I'm thinking i'll first call sort(list)
so that it takes frequency of occurences and places them next to each other. so that i can stop comparing as soon as target doesn't equal the index value. Then once that happens change target to the current index and continue this all the way through the list. I'm having a hell of a time implementing this idea.
int target = list.get(0);
if( target == list.get(i).intValue() )
list.remove(i); // as you go down remove top index
target = list.get(i); // if it is not = to target target becomes next item to count and repeat
will this work?i'm not able to implement it first the ints in the list are new Integer. So when i try to call intValue on the particular index it gives me an error i'v tried casting and everything.
Does this algorithm appear to be possible? is there a better way! This is very important to me so i will be giving max points to help me to get it functioning