[LeetCode] Find the Most Common Response

3527. Find the Most Common Response

You are given a 2D string array responses where each responses[i] is an array of strings representing survey responses from the ith day.

Return the most common response across all days after removing duplicate responses within each responses[i]. If there is a tie, return the lexicographically smallest response.

A string a is lexicographically smaller than a string b if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
If the first min(a.length, b.length) characters do not differ, then the shorter string is the lexicographically smaller one.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
fun findCommonResponse(responses: List<List<String>>): String {
return responses
.map { it.distinct() }
.flatten()
.groupingBy { it }
.eachCount()
.entries
.sortedWith(compareByDescending<Map.Entry<String, Int>> { it.value }
.thenBy { it.key })
.firstOrNull()?.key ?: ""
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/04/27/PS/LeetCode/find-the-most-common-response/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.