Two Laptop Bag*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

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 s.th. 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!)

Thanks,
Sahar.
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.


OCPJP 85%
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

sahar sa wrote:(or s.th. like that)

Please read this http://faq.javaranch.com/java/UseRealWords


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

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
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

sahar sa wrote:(or s.th. like that)

I have no idea what "s.th" means.


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

Joined: Oct 27, 2005
Posts: 19651
    
  18

something


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
 
subject: is there any class which returns the maximum repeated element?
 
Similar Threads
Error #: 361 : variable required, but value found
How to find repeated value in array?
Deleting repeated values in an array
Java Code
Collection.toArray() method