코딩테스트/LeetCode

[LeetCode] Majority Element

ShovelingLife 2023. 12. 19. 14:47
#include <algorithm>

using Intpair = pair<int, int>;

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

for (const auto& item : nums)
	m[item]++;

vector<Intpair> v(m.begin(), m.end());
sort(v.begin(), v.end(), [&](Intpair& lRef, Intpair& rRef)
	{
		return lRef.second < rRef.second;
	});

return (*(v.end() - 1)).first;
    }
};