CCF CSP 求出现次数最多的值

样例:输入 6

                    10 20 10 20 5 5

            输出 5


数组实现:

import java.util.Scanner;  

  
public class minapp
{  
  
    public static void main(String[] args)  
    {  
        new minapp().run_1();  
    }  
  
    public void run_1() //此方法不错,数组下标即为出现的数,对应的值即为出现的次数,但空间开销太大。  
    {  
        Scanner sc = new Scanner(System.in);  
        int n = sc.nextInt();  
        int count[] = new int[10001];
        for (int i = 0; i < n; i++)  
        {  
            ++count[sc.nextInt()];  //count[]相当于value,[]框里的值相当于key
        }  
        int result = -1;  
        int maxCount = -1;  
        for (int i = 0; i < count.length; i++)  
        {  
            if (count[i] > maxCount)  
            {  
                maxCount = count[i];  
                result = i; 
            }  
        }  
        System.out.println(result);  
        sc.close();  
    }  
      
      
}  

你可能感兴趣的:(编程练习题)