본문 바로가기
C++

[프로그래머스] 최빈값 구하기 (수정중)

by 띰쥬 2025. 9. 1.
728x90
반응형
SMALL

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

using namespace std;

int solution(vector<int> array) {
    if(array.size() ==1) return 1;
    
    int answer = 0;
    
    map<int, int> arr_map;
    
    for(auto a : array)
       arr_map[a]++;
    
    for(auto itr = arr_map.begin(); itr != arr_map.end(); itr++) {
        if(arr_map.size()==1) return itr->second;
        
        if(answer < itr->second) 
            answer = itr->second;
        else if(answer == itr->second) 
            answer = -1;
    }
    
    return answer;
}

728x90
반응형
LIST

댓글