高分悬赏!Java语言数组如何将数组中的相同数字合并,并且输出重复数量最多的5个数字
思路我会,就是代码写不出来
高分悬赏!Java语言数组如何将数组中的相同数字合并,并且输出重复数量最多的5个数字
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- libin4 2020-01-12 23:44关注
public static void main(String[] args) { int[] aArr = {1, 2, 3, 4, 5, 6, 4, 5,5, 6, 7, 8, 6, 4, 3, 3, 4, 5, 6, 4, 4, 5, 6, 7, 9, 8, 7, 6, 5, 4, 3}; //去重 int[] ints = Arrays.stream(aArr).distinct().toArray(); for (int anInt : ints) { System.out.print(anInt + ","); } //统计次数 Map<Integer, Integer> map = new HashMap<>(); for (int i : aArr) { Integer count = map.get(i); if (count != null) { ++count; map.put(i, count); } else { map.put(i, 1); } } //排序只取次数最多的5个 List<Map.Entry<Integer, Integer>> collect = map.entrySet().stream() .sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).limit(5).collect(Collectors.toList()); for (Map.Entry<Integer, Integer> entry : collect) { System.out.println("数字:" + entry.getKey() + " 数量:" + entry.getValue()); } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
- ¥15 谁有desed数据集呀
- ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
- ¥15 关于#hadoop#的问题
- ¥15 (标签-Python|关键词-socket)
- ¥15 keil里为什么main.c定义的函数在it.c调用不了
- ¥50 切换TabTip键盘的输入法
- ¥15 可否在不同线程中调用封装数据库操作的类
- ¥15 微带串馈天线阵列每个阵元宽度计算
- ¥15 keil的map文件中Image component sizes各项意思