How is this code for analysing the longest string ...
# codereview
t
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
Copy code
fun getLongestStringFromList(list: List<String>) = list.maxBy { it.length }
K 2
t
@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.