[백준] 11652번 카드 / C++

#문제

백준 11652번 카드

#풀이

#include <iostream>
#include <unordered_map>
#include <limits>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;

	unordered_map<long long, int> arr;
	long long freq_num = numeric_limits<long long>::max();
	int max_freq = 0;

	while (n--)
	{
		long long x;
		cin >> x;

		arr[x]++;

		if (arr[x] > max_freq || (arr[x] == max_freq && x < freq_num))
		{
			max_freq = arr[x];
			freq_num = x;
		}
	}

	cout << freq_num;

	return 0;
}

#정리

n개의 카드를 받는다. 가장 빈도가 많은 카드를 출력한다. 속도를 위해 해시를 사용했다. key값을 받을 때 마다 value를 더해주고, value 값이 가장 큰 수, 또는 value가 같다면 key가 작은 수를 freq_num으로 설정하여 출력하였다. long long freq_num = numeric_limits<long long>::max(); 은 long long의 최대값을 저장하는 스니펫이다.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 9613번 GCD 합 / C++
  • [백준] 1010번 다리 놓기 / C++
  • [백준] 11659번 구간 합 구하기 4 / C++
  • [백준] 1940번 주몽 / C++
  • [백준] 1244번 스위치 켜고 끄기 / C++