After sorting, you can use takeWhile to avoid repeating the second time.
case class Student(grade: Int) val list = List(Student(1), Student(3), Student(2), Student(1), Student(25), Student(0), Student (25)) val sorted = list.sortWith (_.grade > _.grade) sorted.takeWhile (_.grade == sorted(0).grade)
He still sorts everything, even grades 1, 3, 0 and -1, which we are not interested in before taking whipped cream, but this is a short code.
update:
The second approach, which can be performed in parallel, is to split the list and take the maximum of each side, and then only the higher, if any else else:
def takeMax (l: List[Student]) : List [Student] = l.size match { case 0 => Nil case 1 => l case 2 => if (l(0).grade > l(1).grade) List (l(0)) else if (l(0).grade < l(1).grade) List (l(1)) else List (l(0), l(1)) case _ => { val left = takeMax (l.take (l.size / 2)) val right= takeMax (l.drop (l.size / 2)) if (left (0).grade > right(0).grade) left else if (left (0).grade < right(0).grade) right else left ::: right } }
Of course, we would like to separate the student and the method of comparing the two of them.
def takeMax [A] (l: List[A], cmp: ((A, A) => Int)) : List [A] = l.size match { case 0 | 1 => l case 2 => cmp (l(0), l(1)) match { case 0 => l case x => if (x > 0) List (l(0)) else List (l(1)) } case _ => { val left = takeMax (l.take (l.size / 2), cmp) val right= takeMax (l.drop (l.size / 2), cmp) cmp (left (0), right (0)) match { case 0 => left ::: right case x => if (x > 0) left else right } } } def cmp (s1: Student, s2: Student) = s1.grade - s2.grade takeMax (list, cmp)
user unknown
source share