• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there any class which returns the maximum repeated element?  RSS feed

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Please read this http://faq.javaranch.com/java/UseRealWords
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you could write one yourself and i guess it'd take lesser time as compared to searching for one. just my thought
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


I have no idea what "s.th" means.
 
Sheriff
Posts: 21288
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something
 
Bartender
Posts: 1770
45
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!