高分悬赏!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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
- ¥15 C# datagridview 单元格显示进度及值
- ¥15 thinkphp6配合social login单点登录问题
- ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配