• Post Reply Bookmark Topic Watch Topic
  • New Topic

Top Three Strings in array  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I need my code to print out the top three most common IP Addresses in the ArrayList. Right now I have it printing out the top IP Address. I am a little confused as to how to get it to print out three. Every time I change something, It doesn't give me the correct results. Any Suggestions?
My Code:

Thank you
 
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step away from the computer, and figure out how you would do it with pen and paper.

As you have mentioned, it currently calculates the most common ip address, by remembering it's value (mostCommon), and how many there were of them (max).

If you want to keep the top THREE values, then you will need some more variables to store those other values.
One approach would be to declare "secondMostCommon" and "secondMostCommonCount" or similar.
Another approach might be to use a collection or array to store the values within it.

 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that this should be easier than I am thinking it is, just for some reason I am confusing myself. Here do you think that what I have so far to get the top three is enough with adding the new variables or should I try to use a map? I haven't worked too much with lists, sets, or maps.
 
Stefan Evans
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either will work.
It is quite doable with three variables to store the top three items.
The "more variables" approach might be easier for you to implement if you are just starting. Or an Array.

Your algorithm thus far:
- add all the values into a list
- sort the list
- count how many items of each type there are in the list.

Your approach WOULD lend itself very well to using a Map of String (IP Address) to Integer (how many times each one occurs) i.e. Map<String,Integer>. It would let you get rid of the sorting/counting steps as the map would group them for you and counting could be done each time you added an entry.
But even if you DO do that, you will end up at the same point you are now. You can iterate through, and get values and counts one by one, but how do you find and remember the top 3 ?

I think an Array would be the most flexible.



These would replace your mostCommon and max variables.
I would suggest writing a new method



Then instead of just checking the new value against just the maximum so far, you have to check it against (up to) three you have so far.


 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use another list to keep track of each new ”top" item as you find it. When you're done processing the main list, the last three items in your "top" list, if there are that many, will be your top three, in reverse order. That is, most used will be the last item, second-most used the second to the last item and so on.
 
Greenhorn
Posts: 9
Java Netbeans IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Count the duplicate IP Addresses and store them in a Map, then sort the map with respect to the values (Frequency counts) and print the top three.



Now simply print the top three elements in treeMap.
 
Stefan Evans
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Animesh: Careful there. A tree map sorts things by the KEY rather than the value.
So you will get a the first three alphabetical IP addresses, but not necessarily the top three most used.


@Junilu: Just because I'm being nitpicky, how would your algorithm handle the input of counts: 100, 2, 2, 40?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make it even simpler, you could use a list implementation that supports adding elements at a specific index. java.util.ArrayList is one such implementation. If you add each new "top" item that your main loop finds to the front of the "top" list, then you'll have your top three right there at the start of the list. Another option would be to use a stack, which is a last in first out (LIFO) data structure. Lists and stacks are not that hard to understand; with a little bit of reading through the API documentation it shouldn't take much effort to use these in your program.

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your input, I am going to start over and try to put it in a map because that seams a little more simple than what I am currently trying to do.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
@Junilu: Just because I'm being nitpicky, how would your algorithm handle the input of counts: 100, 2, 2, 40?

He would need to keep the part where he sorts the main list first
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Stefan Evans wrote:
@Junilu: Just because I'm being nitpicky, how would your algorithm handle the input of counts: 100, 2, 2, 40?

He would need to keep the part where he sorts the main list first

I just realized that won't work. I guess the best option would be to use a histogram as you suggested and sort it by value in descending order. Good catch.
 
Animesh Shaw
Greenhorn
Posts: 9
Java Netbeans IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:@Animesh: Careful there. A tree map sorts things by the KEY rather than the value.
So you will get a the first three alphabetical IP addresses, but not necessarily the top three most used.


@Junilu: Just because I'm being nitpicky, how would your algorithm handle the input of counts: 100, 2, 2, 40?


Thanks, it totally slipped from my mind.

@OP I have a solution to your problem. Here's the code and I have commented out the main Logic :-



Output



By the Way I have used Java 8 streams and Lambdas. You can simply use for each.


I hope it helps.


Thank you,
Sincerely,
Psycho_Coder.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I point out these two recent threads which also seem to be about counting things in Lists: 1 2. You might find something useful in them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!