Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

counting algorithm

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so maybe you are able to give me an example of clear, easy to understand algorithm?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you do if you did it by hand?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really expecting floating point doubles?

Bill
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floats should be enough.

Can You help?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucas Smith wrote:
Operator == compares the bit pattern.

No.

This prints false:
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this is an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic