• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 22
MyEclipse IDE Hibernate Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good grief. What a dreadful solution.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quickly created something:
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
MyEclipse IDE Hibernate Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
MyEclipse IDE Hibernate Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic