File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is there any class which returns the maximum repeated element?

 
sahar sa
Ranch Hand
Posts: 102
  • 0
  • 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.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • 0
  • 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.

 
Maneesh Godbole
Saloon Keeper
Posts: 10976
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • 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
 
Pramod Chris
Greenhorn
Posts: 21
  • 0
  • 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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something
 
salvin francis
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic