HW算法题:查找众数及中位数

	/*
	 * 查找众数及中位数
	 * 众数是指一组数据中出现次数量多的那个数,众数可以是多个, 中位数是指把一组数据从小到大排列,最中间的那个数
	 * 如果这组数据的个数是奇数,那最中间那个就是中位数,如果这组数据的个数为偶数,那就把中间的两个数之和除以2,所得的结果就是中位数
	 * 查找整型数组中元素的众数并组成一个新的数组,求新数组的中位数
	 * 输入描述:  输入一个一维整型数组,数组大小取值范围 0<N<1000,数组中每个元素取值范围 0<E<1000
	 * 输出描述:  输出众数组成的新数组的中位数
	 * 示例1:
	 * 输入: 10 11 21 19 21 17 21 16 21 18 15
	 * 输出: 21
	 * 
	 * 示例2:
	 * 输入: 2 1 5 4 3 3 9 2 7 4 6 2 15 4 2 4
	 * 输出: 3
	 * 
	 * 示例3:
	 * 输入: 5 1 5 3 5 2 5 5 7 6 7 3 7 11 7 55 7 9 98 9 17 9 15 9 9 1 39
	 * 输出: 7
	 */
	public static void main(String[] args) {
		//int[] arr = {10, 11, 21, 19, 21, 17, 21, 16, 21, 18, 15};
		//int[] arr = {2, 1, 5, 4, 3, 3, 9, 2, 7, 4, 6, 2, 15, 4, 2, 4};
		int[] arr = {5, 1, 5, 3, 5, 2, 5, 5, 7, 6, 7, 3, 7, 11, 7, 55, 7, 9, 98, 9, 17, 9, 15, 9, 9, 1, 39};
		//存储各个元素出现次数
		Map<Integer, Integer> map = new HashMap<Integer, Integer>(arr.length);
		for (int i : arr) {
			if (map.containsKey(i)) {
				map.put(i, map.get(i) + 1);
			}else {
				map.put(i, 1);
			}
		}
		//根据元素出现次数添加众数
		List<Integer> list = new ArrayList<Integer>();
        //定义游标
		int cursor = 0;
		for (Integer key : map.keySet()) {
			Integer count = map.get(key);
			if (count>cursor) {
                //清空list,添加更大的元素, 并替换游标
				list.clear();
				list.add(key);
				cursor = count;
			}else if (count == cursor) {
				list.add(key);
			}else {
				continue;
			}
		}
		//按照从小到大排序
		Collections.sort(list, (n1, n2) -> n1.compareTo(n2));
		int middleIndex = list.size()/2;
		if (list.size()%2 != 0) {
			System.out.println(list.get(middleIndex));
		}else {
			System.out.println(Integer.valueOf((list.get(middleIndex-1) + list.get(middleIndex))/2));
		}
		
	}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值