프로그래머스 120812 최빈값 구하기

Jmnote (토론 | 기여)님의 2023년 12월 1일 (금) 21:06 판
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

프로그래머스 120812 최빈값 구하기

2 C++[ | ]

#include <string>
#include <vector>
#include <map>

using namespace std;

int solution(vector<int> array) {
    map<int,int> m;
    int maxFreq = 0;
    for(auto& x: array) {
        m[x]++;
        if(m[x] > maxFreq) {
            maxFreq = m[x];
        }
    }    
    int answer = -1;
    for(auto it=m.begin(); it!=m.end(); it++) {
        if(it->second == maxFreq) {
            if(answer != -1) {
                return -1;
            }
            answer = it->first;
        }
    }
    return answer;
}

3 같이 보기[ | ]