score:1

Accepted answer

the idea is choosing first element and then removing corresponding elements from list to make sure next choice is unique, as below:

var rankings = new list<rankings> {
    new rankings{  job_id= 4,employee_id= 3, overall_score=  800 },
    new rankings{  job_id= 4,employee_id= 4, overall_score=  800 },
    new rankings{  job_id= 3,employee_id= 1, overall_score=  800 },
    new rankings{  job_id= 3,employee_id= 2, overall_score= 1200 },
    new rankings{  job_id= 2,employee_id= 1, overall_score= 1600 },
    new rankings{  job_id= 2,employee_id= 2, overall_score= 1800 },
    new rankings{  job_id= 4,employee_id= 1, overall_score= 2000 },
    new rankings{  job_id= 4,employee_id= 2, overall_score= 2100 },
    new rankings{  job_id= 1,employee_id= 1, overall_score= 6400 },
};
var cpy = new list<rankings>(rankings);
var result = new list<rankings>();
while (cpy.count() > 0)
{
    var first = cpy.first();
    result.add(first);
    cpy.removeall(r => r.employee_id == first.employee_id || r.job_id == first.job_id);
}

result:

+--------+-------------+---------------+
| job_id | employee_id | overall_score | 
+--------+-------------+---------------+
|      4 |           3 |           800 |
|      3 |           1 |           800 |   
|      2 |           2 |          1800 |
+--------+-------------+---------------+

score:0

basically you could use system.linq.distinct method reinforced with the custom equality comparer iequalitycomparer<ranking>. the system.linq provide this method out of the box.

public class comparer : iequalitycomparer<ranking>
{
    public bool equals(ranking l, ranking r)
    {
        return l.job_id == r.job_id || l.employee_id == r.employee_id;
    }

    public int gethashcode(ranking obj)
    {
        return 1;
    }
}

the trick here is with the gethashcode method, and then as simple as this

rankings.distinct(new comparer())

score:1

really, if you're trying to get the best score for the job, you don't need to select by unique job_id/employee_id, you need to sort by job_id/overall_score, and pick out the first matching employee per job_id (that's not already in the "assigned list").

you could get the items in order using linq:

var sorted = new list<ranking>
( 
  rankings
    .orderby( r => r.job_id )
    .thenby( r => r.overall_score ) 
);

...and then peel off the employees you want...

  var best = new list<ranking>( );
  sorted.foreach( r1 => 
  {
    if ( !best.any
    ( 
      r2 => 
        r1.job_id == r2.job_id 
        || 
        r1.employee_id == r2.employee_id
    ) )
    {
      best.add( r1 );
    }
  } );

instead of using linq to produce a sorted list, you could implement icomparable<ranking> on ranking and then just sort your rankings:

public class ranking : icomparable<ranking>
{
  int icomparable<ranking>.compareto( ranking other )
  {
    var jobfirst = this.job_id.compareto( other.job_id );
    return
      jobfirst == 0?
        this.overall_score.compareto( other.overall_score ):
        jobfirst;
  } 

  //--> other stuff...

}

then, when you sort() the rankings, they'll be in job_id/overall_score order. implementing icomparable<ranking> is probably faster and uses less memory.

note that you have issues...maybe an unstated objective. is it more important to fill the most jobs...or is more important to find work for the most employees? the route i took does what you suggest, and just take the best employee for the job as you go...but, maybe, the only employee for job 2 may be the same as the best employee for job 1...and if you put him/her on job 1, you might not have anybody left for job 2. it could get complicated :-)


Related Query

More Query from same tag