File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes counting algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "counting algorithm" Watch "counting algorithm" New topic
Author

counting algorithm

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    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?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

OK, so maybe you are able to give me an example of clear, easy to understand algorithm?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

What would you do if you did it by hand?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Are you really expecting floating point doubles?

Bill
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Floats should be enough.

Can You help?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    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

Joined: Apr 20, 2009
Posts: 804
    
    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?
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Lucas Smith wrote:
Operator == compares the bit pattern.

No.

This prints false:
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

But this is an exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: counting algorithm
 
Similar Threads
Is there any means to check the number of objects created when a java prgram runs.
Java Questions in Interview
cyclic reference in gc
Need suggestion on implementation
Implement a Garbage Collector