ZZULIOJ 1153: Simple version of the longest sequence, Java

ZZULIOJ 1153: Simple version of the longest sequence, Java

Question description

Given a set of numbers (not sorted), please design a program: find the largest number in it. And output the length of this number.
For example: the numbers given to you are: 1, 2, 3, 3, 4, 4, 5, 5, 5, 6, of which there are only 6 groups of numbers: 1, 2, 3-3, 4-4, 5-5- 5 and 6.
The longest group is 5, with a length of 3. So the output is 3.

enter

The first line is an integer t ((1 ≤ t ≤ 10)), indicating that there are n sets of test data.
Each set of test data includes two lines, the first line is the length of the array n (1 ≤ n ≤ 10000). The second line contains n integers, and the range of all integers Mi is (1 ≤ Mi < 2^32)

output

Corresponding to each set of data, output the length of the largest number.

Sample inputCopy
1
10
1 2 3 3 4 4 5 5 5 6
Sample outputCopy
3
import java.io.*;
import java.util.HashMap;

public class Main {
    
    
    public static void main(String[] args) throws IOException {
    
    
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int T = Integer.parseInt(bf.readLine());
        while (T-- > 0) {
    
    
            HashMap<Long, Integer> hashMap = new HashMap<>();
            int n = Integer.parseInt(bf.readLine());
            String[] str = bf.readLine().split(" ");
            int mx = 0;
            for (int i = 0; i < n; i++) {
    
    
                long x = Long.parseLong(str[i]);
                hashMap.put(x, hashMap.getOrDefault(x, 0) + 1);
                mx = Math.max(mx, hashMap.get(x));
            }
            bw.write(mx + "\n");
        }
        bw.close();
    }
}

Guess you like

Origin blog.csdn.net/qq_52792570/article/details/132568951