Skip to content

Majority Element #193

@fkdl0048

Description

@fkdl0048
#include <unordered_map>

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        unordered_map<int, int> m;

        for (int num : nums) {
            m[num]++;
        }

        int maxCount = 0;
        int maxNum = 0;
        for (auto pair : m) {
            if (pair.second > maxCount) {
                maxNum = pair.first;
                maxCount = pair.second;
            }
        }
        
        return maxNum;
    }
};
  • map으로 풀거나 정렬한 뒤에 개수 찾기

Metadata

Metadata

Assignees

Labels

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions