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!
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.
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"
Joined: Oct 13, 2005
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.
Joined: Sep 24, 2010
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!