JAVA求 出现次数最多数值最大的数

package com.test.com;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * 出现次数最多数值最大的数
 * @author <a href="mailto:qihao@ssreader.cn">chenqihao</a>
 *@version 2013-9-11
 */
public class Test3 {
	public static void main(String[] args) {
		int a[] = new int[] { 1, 3, 5, 5, 2, 6, 1 };
		HashMap hmap = new HashMap();
		// 统计每个数字出现的次数
		for (int i = 0; i < a.length; i++) {
			if (hmap.get(a[i]) == null) {
				hmap.put(a[i], 1);
			}
			else {
				int num = (Integer) hmap.get(a[i]);
				hmap.put(a[i], ++num);
			}
		}
		// 分别存放出现次数最多的次数,及对应的值
		int maxNum = Integer.MIN_VALUE, maxCount = Integer.MIN_VALUE;
		int num;// 存放从集合中得到的Key值
		Set kset = hmap.keySet();
		Iterator ites = kset.iterator();
		while (ites.hasNext()) {
			num = (Integer) ites.next();
			if ((Integer) hmap.get(num) > maxCount) {
				maxNum = num;
				maxCount = (Integer) hmap.get(num);
			}
			else if ((Integer) hmap.get(num) == maxCount) {
				if (num > maxNum) {
					maxNum = num;
				}
			}
		}
		System.out.println("出现次数最多数值值最大的数" + maxNum + "出现" + maxCount + "次");
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值