# Finding Multiple Equal Max Values in an Array

• October 18th, 2011, 11:49 PM
AbsolutelyNobody
Finding Multiple Equal Max Values in an Array
Salutations, all! I'm taking my first Java class in college and working on a personal project on the side so this is not an assignment and not urgent in any way. I've been really impressed with reading the posts on this forum, particular the various guides people have posted. Also a special nod to KevinWorkman for helping me out last time, thanks again! :)

I tried searching the forums for a similar thread but apparently "max" and "value" are too common to search for and "max value in an array" seemed to return many "array" threads but not any relevant ones. I apologize in advance if this is a redundant problem, all I've been able to find on the subject is how to find a single max value in the array.

The Program
I have an array of Player objects (created at the start of the program, can be any valid int in length) which have an int Number and int Score. I need to find the highest score in the array and record that Player's Number and Score. Here's my code right now:

Code :

``` // Player with highest score wins for (counter = 0; counter < players.length; counter++) { System.out.println("Player" + players[counter].getNumber() + " 's final score is " + players[counter].getScore() );   if (players[counter].getScore() > winnerScore){   winnerScore = players[counter].getScore(); winner = players[counter].getNumber();   }   else{ } }   System.out.println( "Player" + winner + " wins with " + winnerScore + " points!" );```

The Problem
This seems to work how I want it to except it has no way of accounting for ties, i.e. when more than one player has the same score. Because the for loop goes through the array entry by entry the first player with the high score will always come out as the winner.

Possible Solution
I believe any solution to this problem requires creating a new array of Player objects for winning players equal to the original Player array's length, since it's possible for all players to be tied. I could use an if statement to move player objects to this new array (if their score was >= the current highest score) but since the for loop starts at the beginning of the array it would always get Player1 and would get other players that didn't necessarily have the highest score-just the highest to that point.

If I could create a winning array and move Players objects with a Score => winnerScore to it while also removing any Player objects with a Score < the Score of the new Player object being added I believe that would work. In order to do that I would need to have another for loop go through the winner's array and find the max score there, then remove all Player objects with a lower Score? That seems like it's overly redundant to me.

Another Possible Solution
Sorry if these seem a bit stream-of-conscious but I'm just thinking of them as I post this message. If I had the code above find the winnerScore, I could have another for loop go through the array and assign all players with a Score equal to winnerScore to a new winner's array, then print every Player object's number and score in that array? Now that I've thought of it that seems like the right thing to do, but I'm wondering if it's possible to do it in a single loop.

Unfortunately I do not have a Java editor on the computer I am at now or else I would try the latter method above which I do think will work if I'm thinking of it right. However I'd really like to keep the coding as simple as possible and I'm still unable to think of a single-loop method. Again this is just for fun so I don't mean to be a burden, but I'm excited because I got such a quick and helpful response last time.

In any case thanks again to all the people who help out on this forum, it's great to know there's a place like this if something seriously important ever did come up. :)
• October 19th, 2011, 04:13 AM
Tsarin
Re: Finding Multiple Equal Max Values in an Array
What you could do is create an ArrayList of Player(s), add the first player to the array list. Iterate through the rest of the players and check if their score is greater than the score of the first entry in the array list, if it is, clear the array list and add the current player. Otherwise, if the current players score is equal to the score of the first entry in the array list, add them too. (Explaining in english is so much harder than diagrams or code!)

This will let you store the whole Player object, and you could do some cool stuff around keeping a score history or stats for each player, etc.

Hopefully that is what you mean :)
• October 26th, 2011, 03:53 PM
kenster421
Re: Finding Multiple Equal Max Values in an Array
Using an ArrayList seems like overkill.

I think you should use multiple methods:

int getHighestScore()
Player[] getWinners()
void printWinners()

Inside the getWinners() method, you create an array and add players to the new array if their score equals the high score.

Inside printWinners(), you traverse the array created from the getWinner() method in a loop and print out what you need.

Simple.

No ArrayList or extra datastructures.
• October 26th, 2011, 06:37 PM
AbsolutelyNobody
Re: Finding Multiple Equal Max Values in an Array
Thanks for the help you guys, I really appreciate it! I wound up doing a version of the alternate possible solution in my first post, which is pretty much the same thing both of you guys recommended (albeit I've not touched arrayLists yet and used an array). I think my problem was trying to do it all in a single For loop and single array, once I moved beyond that it was easy.

Glad to know the people on this forum are so helpful! Thanks again, and good luck with everything!
• October 30th, 2011, 03:07 AM
abani
Re: Finding Multiple Equal Max Values in an Array
Hi,
I think for a better solution a generic class should be created for this to solve it using Reflection too.

bean factory