https://kotlinlang.org logo
#codereview
Title
# codereview
t

therealbluepandabear

03/09/2021, 4:33 AM
How is this code for analysing the longest string in a list:
Copy code
fun getLongestStringFromList(list: List<String>): String {
    val sizesDict = mutableMapOf<String, Int>()

    for (string in list) {
        sizesDict[string] = string.length
    }
    val max = sizesDict.values.indexOf(sizesDict.values.maxOrNull())
    return sizesDict.keys.elementAt(max)
}
m

Milan Hruban

03/09/2021, 8:54 AM
Copy code
fun getLongestStringFromList(list: List<String>) = list.maxBy { it.length }
K 2
t

therealbluepandabear

03/10/2021, 1:01 AM
@Milan Hruban Thanks for this! If I was your boss I'd give you a raise. 🤣
@Milan Hruban Also note that 'maxBy' is deprecated. 'maxByOrNull' is used instead.
2 Views