JAVA CCF-201312-1 出现次数最多的数

欢迎访问我的CCF认证解题目录

题目描述

思路过程

定义一个数组存储数字出现的次数,用for循环从小到大遍历,定义一个indexcount存储当前的下标和次数,如果次数大于当前次数,则重新记录下标和次数

代码

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] number = new int[10005];//存储每个数出现的次数
		int index = 0;
		int count = 0;
		while ( n-- != 0 ) number[in.nextInt()]++;
		

		for ( int i = 0; i < number.length; i++ ) {
			if ( number[i] > count ) {//次数更大
				index = i;
				count = number[i];
			}
		}
		System.out.println(index);
	}
	
}
发布了60 篇原创文章 · 获赞 0 · 访问量 2151

猜你喜欢

转载自blog.csdn.net/weixin_43732798/article/details/100705344
今日推荐