Determining the final winners and all of its losers in a list of win/lose pairs

I'm having trouble figuring out the logic for the following problem - the application is a de-duping process that has to merge the contents of the duplicates:

I have a list of pairs of winners and losers


9962261 wins over 9962260
9962263 wins over 9962261
9962263 wins over 9962260

In this example,  9962263  is the winner, and wins out over 9962260 and 9962261.

imagine thousands of such contests expressed as records like this

how do i algorithmically end up with an array of the form

winner1, [list of losers]
winner2, [list of losers]
winner3,  [list of losers]

such that there is no winner that is also a loser.

My logic fails me.
Who is Participating?
TommySzalapskiConnect With a Mentor Commented:
I don't know exactly what you are trying to do with this, but here is how I would most likely approach the problem.

Build a tree structure. Have one root node that originally has every person as a child node. Then, any time one person wins over another, move the loser off the root node and under the winner node. This way you don't have any duplication in the tree (each player is only one node) but you have the outcome of every match stored.
Also your idea of a list of losers under each undefeated winner is maintained in the entire tree under the winner.
Ray PaseurCommented:
Just looking at this...

9962261 wins over 9962260
9962263 wins over 9962261
9962263 wins over 9962260

It would seem that you can take all the numbers in the right column and remove any instance of them from the left column.  That will tell you the winners who are not also losers.

Not sure I understand the other part.  How would you know whether 9962263 eliminated 9962260 before 9962261 eliminated 9962260.  That detail would seem to have an effect on whatever you wanted to put into the list of losers.
Marco GasiFreelancerCommented:
I don't know if I'm able enough to help you, but sure I (and all other experts, I think) need to know how is formatted your original list: is it an ssociative array where the key is the winner and the value the looser? Or is it something else?

And the result you wish is, following your example, an array of array as the following one?

 9962263 => array(9962260, 9962261)
 9962261 => array(9962260)
 9962260 => array()

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Kevin CrossChief Technology OfficerCommented:
How is the database laid out. Is it two columns: '9962261', '9962260' (where the right is the loser). Or is it one column: '9962261 wins over 9962260'?

Either way, I would implement a WHERE clause that only pulled values on the LEFT that do not appear on the RIGHT. For one field, you can use LEFT() with an INSTR, CHARINDEX, or similar type method depending on your database platform -- what is that by the way? -- then to compare to the other side, you can use similar functions to pull the right portion or use REGEXP if MySQL.

From there, you can use a GROUP BY and database specific tricks like GROUP_CONCAT or FOR XML to generate a common-delimited list for the losers.
metalaureateAuthor Commented:
Ingenious. Thanks to all who helped.
Ray PaseurCommented:
Please show us the data structure and how you programmed the solution, thanks. ~Ray
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.