|| algorithm to train most of Yabo sports integer number appears (HashMap)

 Write a program that reads a set of integers, which is the group of integers in the ascending order of the number N which is input by the user, up to no more than 20.
|| algorithm to train most of Yabo sports integer number appears (HashMap)
The program then this will be an array of statistics, the highest number of occurrences of that array element value printed out. If two elements have the same number of value occurs, that is tied for first, then print only the relatively small value.
  Input format: The first line is an integer N, N <20; Then there are N rows, each row represents an integer, and the ascending order in accordance.
  Output formats: Output only one line, that is, the element values most often appears.
Sample input and output
sample input
. 5
100
150
150
200 is
250
samples output
150

public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer,Integer> map = new HashMap<Integer,Integer> ();
Scanner in=new Scanner(System.in);
int n=in.nextInt(); //输出N
int arr[]=new int[n];
int save=0,count=0; //save保存次数最多的整数
for(int j=0;j<n;j++)
arr[j]=in.nextInt();

    //把键值放入HashMap中 
    for(int word :arr) {
        if(map.containsKey(word))
            map.put(word, map.get(word)+1);
        else {
            map.put(word, 1);
            count++;    //种类
        }
    }
    save=arr[0];    //先让第一个元素赋值给save,用于多个数据次数一致选最小
    for(int i=1;i<count;i++) {              
        if(map.get(arr[i])>map.get(arr[i-1])) { //若有元素的次数更大,更新save      
            save=arr[i];
            }
    }
    System.out.println(save);
}

Guess you like

Origin blog.51cto.com/14070383/2471175