작성일 :

문제 링크

9913번 - Max

설명

수열에서 가장 많이 등장하는 값의 빈도를 출력하는 문제입니다.


접근법

각 수의 등장 횟수를 세면서 최대 빈도를 갱신합니다.
모든 입력을 처리한 뒤 최대 빈도를 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var cnt = new int[1001];
    var best = 0;
    for (var i = 0; i < n; i++) {
      var x = int.Parse(Console.ReadLine()!);
      cnt[x]++;
      if (cnt[x] > best) best = cnt[x];
    }
    Console.WriteLine(best);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

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

  int n; cin >> n;
  int cnt[1001] = {};
  int best = 0;
  for (int i = 0; i < n; i++) {
    int x; cin >> x;
    cnt[x]++;
    if (cnt[x] > best) best = cnt[x];
  }
  cout << best << "\n";

  return 0;
}