How to find max in a list of tuples?

前端 未结 3 1676
不思量自难忘°
不思量自难忘° 2020-12-06 10:00

I have the following list of tuples:

val arr = List((\'a\',10),(\'b\',2),(\'c\',3))

How to find the tuple with the max key or max value?

3条回答
  •  清歌不尽
    2020-12-06 10:36

    No doubt @om-nom-nom provided a concise, correct answer. However, it will throw an exception for an empty list.

    EDIT #2 Given my first edit, it's worthwhile to re-write my original, flawed answer:

    def max[A](list: List[(A, Int)]): Option[Int] = list match  {
        case Nil => None
        case x :: xs => { val result = xs.foldLeft(x._2) { // acc = first item in list                                        
                                  (acc, elem) => if(elem._2 > acc) elem._2 else acc 
                          }
                          Some(result)
                        }
    } 
    

    Note: I'm guessing that scalaz would let you use a more generic Num-like type instead of Int, but I haven't worked with it at all.

    Testing

    scala> val list = List(('a',10),('b',2),('c',3))
    list: List[(Char, Int)] = List((a,10), (b,2), (c,3))
    
    scala> max(list)
    res5: Option[Int] = Some(10)
    
    scala> val list: List[(String, Int)] = Nil
    list: List[(String, Int)] = List()
    
    scala> max(list)
    res6: Option[Int] = None
    

    EDIT For picking a start value, I decided to edit my answer after talking with @DustinGetz.

    Picking Int.MinValue might not be a good choice as it's dependent on the particular OS/system on which the app is running.

    I would argue that the first element in the list should be the start value. However, there's a potential run-time exception if the list is empty.

    Please take a look at this post for more discussion - https://stackoverflow.com/a/23184020/409976.

提交回复
热议问题