# counting algorithm

Lucas Smith
Ranch Hand
Posts: 808
1
Hi
I am lookig forward for a fast counting algorithm.

I have an array:
double[] num;

Inside that array are numbers. I have to get the number that occurs the highest number of times.

Any ideas?

fred rosenberger
lowercase baba
Bartender
Posts: 12230
36
how fast does it need to be, and why?

Most of the time, you'll be better served to write the cleanest, easiest to understand code you can. More time is spent on debugging code that anything else. If you write your algorithm in such a fancy way that it saves you .00001 seconds on each run, but it takes you 10 hours to debug, you'd have to run that code...a LOT of times before it was worthwhile.

Lucas Smith
Ranch Hand
Posts: 808
1
OK, so maybe you are able to give me an example of clear, easy to understand algorithm?

fred rosenberger
lowercase baba
Bartender
Posts: 12230
36
What would you do if you did it by hand?

William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
Are you really expecting floating point doubles?

Bill

Lucas Smith
Ranch Hand
Posts: 808
1
Floats should be enough.

Can You help?

fred rosenberger
lowercase baba
Bartender
Posts: 12230
36
I am helping. How would you do it if you had nothing but paper and pencil, and a logbook with numbers in it?

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
And note that the concept of "equals" is pretty fuzzy for doubles (or floats). Are 1 and 1.0001 equal? How about 1 and 1.00000000000001? How about 0 and 1 x 10^-307? You have to decide what equals means.

Anyway, play along with Fred -- he wants to help you figure it out.

Lucas Smith
Ranch Hand
Posts: 808
1
OK, I decided to use shorts for the first part. It will be faster to check the equality of two shorts rather than two floats.

Lucas Smith
Ranch Hand
Posts: 808
1
Just let me think loudly...

I can use floating point numbers as well as integers. I have to compare those numbers. Operator == compares the bit pattern.
Float is 32 bit and Int is 32 bit. So there will be no difference in performance. Am I right?

Ranch Hand
Posts: 245
Lucas Smith wrote:
Operator == compares the bit pattern.

No.

This prints false:

Lucas Smith
Ranch Hand
Posts: 808
1
But this is an exception.