Statistics

During a scientific research investigation, n natural numbers were obtained, each of which is not more than 1500000000 (1.5*10^9). It is known that there are no more than 10,000 different numbers. Now it is necessary to count the number of occurrences of these natural numbers, and output the statistical results in ascending order of the natural numbers.
Input
Line 1 is the integer n, which represents the number of natural numbers.
Lines 2~n+1 have one natural number per line.
Output
Contains m lines (m is the number of different numbers among n natural numbers), and outputs the natural numbers in ascending order. Each line outputs two integers, the natural number and the number of times the number appears, separated by a space.
Sample input
8
2
4
2
4
5
100
2
100

Sample output
2 3
4 2
5 1
100 2

import java.io .*;
import java.util.Scanner;
class Main 
{
    public static void main(String[] args) 
    {
            int n,m, i=0,max=0;
            int a [] =  new int[10000];
            Scanner scan=new Scanner(System.in);
            n=scan.nextInt();
            while(i<n)
            {
                i++;
                m = scan.nextInt();
                a[m]++;
                if(m>max)
                    max=m;
            }
            for(int j=0;j<=max;j++)
            {
                if(a[j]!=0)
                {
                    System.out.println(j+" "+a[j]);
                }
            }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325192014&siteId=291194637