【发布时间】:2021-02-27 08:32:03
【问题描述】:
我想问一下如何找到二维 String 数组中出现频率最高的元素以及该元素在 Java 中出现的次数?
更具体地说:
public static void mostFrequentElement(String[][] data) {
// What should I write here?
}
String[][] data = {
{"apple", "orange", "orange"},
{"grape", "orange", "melon"},
};
mostFrequentElement(data);
// The function prints "orange 3" in the console
// This is because "orange" is the most frequent
// element in the data array and it showed up 3 times
我尝试使用HashMap,但我对它感到困惑,因为我是 Java 的初学者。非常感谢!
【问题讨论】:
标签: java arrays string multidimensional-array