1. The number with the most occurrences

  1. Number with the most occurrences
    Problem description
    Given n positive integers, find the number with the most occurrences; if there are multiple such numbers, please output the smallest one; the first line of the
    input format
    has only one positive integer n(1 ≤ n ≤ 1000), indicating the number of digits;
    the second line of input has n integers s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n); the adjacent numbers are used Space separated;
    output format
    Output the number with the most occurrences among the n times; if there are multiple such numbers, output the smallest one;
    sample input
    6
    10 1 10 20 30 20
    sample output
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    
    
    int n, s, t = 1;
    int a[1001] = {
    
    0};
    cin>>n;
    for(int i = 0; i < n; i++)
    {
    
    
        cin>>s;
        a[s]++;
        if(a[s] > a[t])
        t = s;
        if(a[s] == a[t])
        t = (s < t ? s : t);
    }
    cout<<t<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/KO812605128/article/details/113356809