7-3 Find the number with the most occurrences in the sequence of integers

7-3 Find the number with the most occurrences in the sequence of integers

This question requires counting the most frequently occurring integer and the number of occurrences in an integer sequence.
Input format:
Input the number of integers N (0<N≤1000) in the sequence given in one line, and N integers. Numbers are separated by spaces.
Output format:
Output the integer with the most occurrences and the number of occurrences in one line, and the numbers are separated by spaces. The title guarantees that such numbers are unique.

problem solving ideas

Count the number of occurrences of numbers through STL's map

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
map<int, int> ma;
int main()
{
    
    
	int n;
	cin >> n;
	int num[1000],a,time=0;
	for (int i = 0; i < n; i++)
	{
    
    
		cin >> num[i];
		ma[num[i]] = 0;
	}
	a = 0;
	for (int i = 0; i < n; i++)
	{
    
    
		ma[num[i]] += 1;
		if (ma[num[i]] > time)
		{
    
    
			time = ma[num[i]];
			a = num[i];
		}
	}
	cout << a << " " << time << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43666766/article/details/109470056