• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting most frequent String of objects in array  RSS feed

 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have an array of Trips, and i need to get the most popular guide of the trip.
What i've written below works, but i have a problem when i have two guides that have the same amount of trips.
this is a part of a homework.


 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess i want it to return null, in the case where i have 2 guides that are most popular
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please indent your code properly.
at line 8 you are using "==" that will return true only when the 2 reference points to the same String object.but 2 different String object can also be equal so use its equals method.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:please indent your code properly.
at line 8 you are using "==" that will return true only when the 2 reference points to the same String object.but 2 different String object can also be equal so use its equals method.


ok, fixed that, thanks,  still having main issue.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think they just told us we can return one of the the most popular.
but i still wonder if it is possible with simple coding, or do i need to be advanced in java in order to do it?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you are correct and being a programmer this a plus point.you should consider all the cases but their may be the constraint like only such cases were taken where the popularity will not clash(or even if they clash then then their may be some basis to opt b/w those).Does they mentioned some constraint ? even if they not try working on it by assuming some criteria to handle those cases like you say you will return null.
dan wrote:do i need to be advanced in java in order to do it?
nope a basic knowledge will work..
Are you familiar with collection and map...
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:No you are correct and being a programmer this a plus point.you should consider all the cases but their may be the constraint like only such cases were taken where the popularity will not clash(or even if they clash then then their may be some basis to opt b/w those).Does they mentioned some constraint ? even if they not try working on it by assuming some criteria to handle those cases like you say you will return null.
dan wrote:do i need to be advanced in java in order to do it?
nope a basic knowledge will work..
Are you familiar with collection and map...


i'm not a programmer, if you meant it for me . and not familliar with collection and map, we just learned up to Arrays, still more to come. They asked to get the mos popular guide, and i've just seen that they want use to return either of the most popular, which what i've written worked.
I guess map and collections are to come later.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still their is no problem.be in touch with ranch,then you will become a programmer one day,at least make you feel that you are... .
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you are iterating does not met your need.
like for example suppose we have
Array=["dan","kom","praveen","kumar","dan","kom"]
accordingly with your code the first loop will took "dan" and check for the equality with others(but in your case you are comparing it with even itself) where it will find it true,don't you think it should compare it with others,next thing-your 1st loop again took the 2nd "dan" and again check for its equality but it should not(as this case is already considered when we check for its equality first time).
what is the solution?-i will not post the solution as it is your homework not mine
you can use the copy of an array and whenever equality holds for others(don't compare with itself) i.e.,if true remove the compared element by setting it null and increase the counter by 1.when the iteration ends for 1st element store the counter in an int array(store the counter for each element so that you can figure out which element repeats how many time,counter stands here for the no of time elements occured).repeat this procedure for all the elements and
finally...i left this part for you??

NOTE:you can make use of java.util.Arrays class to copy an array.it is an utility class to deal with array.use its clone or copyOf method.
hope it helps!

Kind Regards,
Praveen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!