aspose file tools
The moose likes Java in General and the fly likes how to count and return the object that occurs the most in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how to count and return the object that occurs the most in an ArrayList" Watch "how to count and return the object that occurs the most in an ArrayList" New topic
Author

how to count and return the object that occurs the most in an ArrayList

Anis Yusof
Greenhorn

Joined: Sep 24, 2010
Posts: 3
Hello everyone.

I have a problem. I would like to know, how to count for duplicate object, and return the highest occurence of object in an ArrayList. For this instance, I am using ArrayList <Integer>; I believed it is also possible for me to use int []. Anyway, these are the sample codes.



In the above codes, there are three 1s, two 2s, and probably other integer that is randomly added. Hence, i want it to return me the Integer 1 object; since Integer 1 occurs the most in this ArrayList.

I have googled it, and searched this forum. But unfortunately, I didn't find what I want. Please help. Thanks!
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

I hope you want to search for the objects containing same value, not the same object.
You are adding a new object every time.

For your problem you need to traverse the entire list and search for the most occurences of the value.
Other option is you can use the search algorithms.

Searching Algorithms


SCJP 5, SCWCD 5
Gupta Tarun
Greenhorn

Joined: Sep 16, 2010
Posts: 22

If Performance is not a concern the following code should work..



Please refer to following interesting thread to understand sorting part...Sorting Tree by Value and not key

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Good grief. What a dreadful solution.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Quickly created something:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Campbell Ritchie wrote:Good grief. What a dreadful solution.

I didn't even delete it like I usually do if someone provides a complete answer.

Like Wouter's complete answer--and I know he knows better :p
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Yea I know and usually I only give abstract answers but his time it was nice to think back to my data-structure lessons and prove to myself that I still got it
Gupta Tarun
Greenhorn

Joined: Sep 16, 2010
Posts: 22

Campbell Ritchie wrote:Good grief. What a dreadful solution.


I Agree. I think its was an overkill if you just need the highest frequency, but okay if you need all the frequencies in descending order.
Good to know about the rules with respect to the posts getting deleted.. as I am new here.

Thanks,
Gupta Tarun
Greenhorn

Joined: Sep 16, 2010
Posts: 22

Wouter Oet wrote:Yea I know and usually I only give abstract answers but his time it was nice to think back to my data-structure lessons and prove to myself that I still got it


data-structure ? or you mean "Flow control"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
David Newton wrote: . . . I didn't even delete it like I usually do if someone provides a complete answer. . . .
Nor did I, for the same reason.
Anis Yusof
Greenhorn

Joined: Sep 24, 2010
Posts: 3
Thank you gupta_tarun & wouter_oet! Both of you really helps me a lot; except both solution take a different approach. It is certainly good learning experience for me to be introduced to Map, Hashmap etc. Never heard & used of hashmap before.. :P anyway, i used wouter solution in my codes. Kinda neat & easy to understand, and it works! thanks again guys!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Gupta Tarun wrote:data-structure ? or you mean "Flow control"

That class also contained algorithms. So that's why I associated it with that class.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to count and return the object that occurs the most in an ArrayList
 
Similar Threads
Performing Math on Integer Class
Question about Tree Node --Word Tree
How to make combination function generic
Recursion-Prime factors
SQL Count