File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes is there any class which returns the maximum repeated element? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is there any class which returns the maximum repeated element?" Watch "is there any class which returns the maximum repeated element?" New topic

is there any class which returns the maximum repeated element?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
Is there any kind of predefined method or class that ask as input an array(or like that) and returns as output, the most repeated element in the input array?

e.g.: input array : conceder, linear,non, conceder,linear,non,non,non
output: non (beacuse it has been repeated more times than the others did!)

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
No, but I think you could make one pretty easily.

Lets say you had an array of strings as in your example.

You could create the following class:

You could then create a class with a method that took your string array as a parameter. The method would loop through the array and for every string it would search to see if a MyClass object with that string was present in a hash table. If it wasn't, then it would create a MyClass object as defined above, set the string value to the value of your string and set the elementCount to 1. If a MyClass object was found with an element variable with your string value, then you would increment the elementCount variable of that object by 1.

After the method had gone through the entire array, you could easily find the MyClass object with the highest elementCount value and the corresponding string value in that object.

I hope that makes sense. Theres probably loads of more elegant solutions, but thats one way you could do it.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10824

sahar sa wrote:(or like that)

Please read this

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Pramod Chris

Joined: Oct 15, 2004
Posts: 21
I guess you could write one yourself and i guess it'd take lesser time as compared to searching for one. just my thought
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

sahar sa wrote:(or like that)

I have no idea what "" means.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271


How To Ask Questions How To Answer Questions
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 1114

use a hashmap that stores the word as key and count as the value,
you can later iterate through the values to find the largest one.

I guess you can even use a comparator with a custom bean in an ArrayList i didn't give it much thought though.

I agree. Here's the better link:

I agree. Here's the link:
subject: is there any class which returns the maximum repeated element?
It's not a secret anymore!