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!
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
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
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"
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
4
posted
0
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
posted
0
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!